【问题标题】:Cakephp Data Validation: Checking if at least one field is populated and multiple rules not validatingCakephp 数据验证:检查是否至少填充了一个字段并且多个规则未验证
【发布时间】:2014-07-15 14:50:40
【问题描述】:

伙计们,

我正在尝试确保至少填充两个字段(姓氏或电子邮件)中的一个。每个字段也有多个规则。我正在使用 CakePHP 2.4.2 版。

我目前遇到的问题是,在多次更新和/或移动使用“last”、“allowEmpty”、“required”等之后,这些字段根本没有验证,或者填充字段时未执行所有规则。

非常感谢任何有关如何修改以下代码以实现以下行为的建议: 1. 必须填写两个字段之一; 2. 附加到每个字段的其他规则也必须有效(即,如果姓氏通过,则它必须介于 2 到 45 个字符之间,并且只能包含字母数字)

这是模型代码:

public $validate = array(
    'last_name'=> array(
        'needOne' => array (
            'required' => FALSE,
            'allowEmpty' => TRUE,
            'last' => TRUE,
            'rule' => array('checkOne','last_name'),
            'message' => 'You must enter at least a contact name or email address.'
         ),
        'alphaNumeric' => array(
            'rule'   => 'alphaNumeric',
            'message'  => 'Alphabets and numbers only'
        ),
        'between' => array(
            'rule'  => array('between', 2, 45),
            'message' => 'Between 2 to 45 characters'
        )
    ),
    'email' => array(
        'needOne' => array (
            'required' => FALSE,
            'allowEmpty' => TRUE,
            'last' => TRUE,
            'rule' => array('checkOne','email'),
            'message' => 'You must enter at least a contact name or email address.'
         ),
        'emailAddress' => array (
            'last' => TRUE,
            'rule' => array('email', FALSE),
            'message'  => 'A valid Email address is required'
         )
    )
);

// Ensure at least the last name or email field value is provided
function checkOne($field) {
    if(!empty($this->data[$this->User]['last_name']) || 
        !empty($this->data[$this->User]['email'])){
        return true;
    } else {
        return false;
    }
}

提前致谢!

【问题讨论】:

    标签: validation cakephp


    【解决方案1】:

    非常感谢vicocamacho。我坚持下去,除了您的建议外,还发现解决方案还在于在视图中添加“必需”=> false。

    对于遇到此问题的其他人来说,这是可行的解决方案:

    型号:

    public $validate = array(
        'last_name'=> array(
            'needOne' => array (
            'rule' => 'checkOne',
            'message' => 'You must enter at least a contact name or email address.'
        ),
        'alphaNumeric' => array(
            'rule' => 'alphaNumeric',
            'message'  => 'Alphabets and numbers only',
            'allowEmpty' => TRUE
        ),
        'between' => array(
            'rule' => array('between', 2, 45),
            'message' => 'Between 2 to 45 characters',
            'allowEmpty' => TRUE
        )
    ),
    'email' => array(
        'needOne' => array (
            'rule' => 'checkOne',
            'message' => 'You must enter at least a contact name or email address.'
        ),
        'emailAddress' => array (
            'rule' => 'email',
            'message' => 'A valid Email address is required',
            'allowEmpty' => TRUE
        )
    )
    );
    
    // Ensure at least the last name or email field value is provided
    public function checkOne($data) {
        if(!empty($this->data[$this->alias]['last_name']) 
        || !empty($this->data[$this->alias]['email'])) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
    

    视图/字段(我正在使用 Bootstrap):

    echo $this->Form->input('last_name', array(
            'required' => false,
            'fieldset' => false,
            'label' => false,
            'before' => '<label class="control-label">Last Name <span class="one-required">*</span></label>',
            'class' => 'form-control',
            'placeholder' => 'Last Name',
            'div' => 'form-group col-sm-12',
            'error' => array(
                'attributes' => array(
                    'wrap' => 'div', 
                    'class' => 'alert alert-danger'
                )
            )
        )
    );
    
    echo $this->Form->input('email', array(
            'required' => false,
            'fieldset' => false,
            'label' => false,
            'before' => '<label class="control-label">Email <span class="one-required">*</span></label>',
            'after' => '',
            'class' => 'form-control',
            'div' => 'form-group col-sm-12 col-xs-12',
            'error' => array(
                'attributes' => array(
                    'wrap' => 'div', 
                    'class' => 'alert alert-danger'
                )
            )
        )
    );
    

    谢谢。

    【讨论】:

      【解决方案2】:

      编写一个自定义规则,该规则将检查两个字段中的一个是否不为空,并在这两个字段上使用该规则。并将其他规则的 allowEmpty 设置为 true。

      【讨论】:

        【解决方案3】:

        您应该在所有不检查字段是否存在的规则中允许Empty,我更新了您的代码以便您了解我的意思,您还需要将checkOne 中的$this-&gt;User 索引更改为@987654323 @ 否则会一直返回false:

        public $validate = array(
            'last_name'=> array(
                'alphaNumeric' => array(
                    'rule'   => 'alphaNumeric',
                    'message'  => 'Alphabets and numbers only',
                    'allowEmpty' => true
                ),
                'between' => array(
                    'rule'  => array('between', 2, 45),
                    'message' => 'Between 2 to 45 characters',
                    'allowEmpty' => true
                )
            ),
            'email' => array(
                'needOne' => array (
                    'rule' => 'checkOne',
                    'message' => 'You must enter at least a contact name or email address.'
                ),
                'emailAddress' => array (
                    'allowEmpty' => true,
                    'rule' => 'email',
                    'message'  => 'A valid Email address is required'
                )
            )
        );
        
        // Ensure at least the last name or email field value is provided
        function checkOne($field) {
                if(
                    !empty($this->data[$this->alias]['last_name']) ||
                    !empty($this->data[$this->alias]['email'])
                )
                {
                        return true;
                } else {
                        return false;
                }
        }
        

        【讨论】:

        • 非常感谢vicocamacho。我坚持下去,除了您的建议外,还发现解决方案还在于在视图中添加“必需”=> false。我在下面包含了解决方案(太长,无法作为评论:))。
        猜你喜欢
        • 1970-01-01
        • 2017-05-02
        • 2010-12-10
        • 1970-01-01
        • 1970-01-01
        • 2012-04-14
        • 1970-01-01
        • 1970-01-01
        • 2013-10-04
        相关资源
        最近更新 更多