Cakephp数据验证:检查是否填充了至少一个字段且多个规则未验证

时间:2014-07-15 14:50:41

标签: validation cakephp

民间,

我正在尝试确保填充两个字段(last_name或email)中的至少一个。每个字段也有多个规则。我正在使用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;
    }
}

提前致谢!

3 个答案:

答案 0 :(得分: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'
            )
        )
    )
);

感谢。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

你应该在所有不检查字段是否存在的规则中允许使用,我更新了你的代码以便你可以看到我的意思,你还需要更改$this->User中的checkOne索引}到$this->alias否则它将始终返回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;
        }
}