【问题标题】:cakephp Custom validation rule messagecakephp 自定义验证规则消息
【发布时间】:2011-04-29 01:45:19
【问题描述】:

我有一个自定义验证规则来检查输入的两个密码是否相同,如果它们不同,我希望有一条消息说“密码不匹配”。

该规则有效,但是,当密码不匹配时,它只会显示正常的错误消息,这是怎么回事?

var $validate=array(
        'passwd2' => array('rule' => 'alphanumeric',
                        'rule' => 'confirmPassword',
                        'required' => true,
                        'allowEmpty'=>false));

function confirmPassword($data)
{
    $valid = false;
    if ( Security::hash(Configure::read('Security.salt') .$data['passwd2']) == $this->data['User']['passwd'])
    {
        $valid = true;
        $this->invalidate('passwd2', 'Passwords do not match');
    }
    return $valid;
}

上面写着“此字段不能留空”

编辑:

奇怪的是,如果我将其中一个密码字段留空,两条错误消息都会显示“此字段不能留空”

但是,如果我在两者中都放了一些东西,那么它会正确显示“密码不匹配”

【问题讨论】:

    标签: cakephp validation


    【解决方案1】:

    您应该使用 $validate 数组中的 'message' 键来指定消息:

    'message' => 'Your passwords do not match'
    

    延伸阅读:http://book.cakephp.org/view/1143/Data-Validation

    【讨论】:

      【解决方案2】:

      我认为你把它弄得太复杂了。这是我的做法:

          // In the model
          public $validate = array(
              'password' => array(
                  'minLength' => array(
                      'rule' => array('minLength', '8')
                  ),
                  'notEmpty' => array(
                      'rule' => 'notEmpty',
                      'required' => true
                  )
              ),
              'confirm_password' => array(
                  'minLength' => array(
                      'rule' => array('minLength', '8'),
                      'required' => true
                  ),
                  'notEmpty' => array(
                      'rule' => 'notEmpty'
                  ),
                  'comparePasswords' => array(
                      'rule' => 'comparePasswords' // Protected function below
                  ),
              )
          );
          protected function comparePasswords($field = null){
              return (Security::hash($field['confirm_password'], null, true) === $this->data['User']['password']);
          }
      
      // In the view
      echo $form->input('confirm_password', array(
          'label' => __('Password', true),
          'type' => 'password',
          'error' => array(
              'comparePasswords' => __('Typed passwords did not match.', true),
              'minLength' => __('The password should be at least 8 characters long.', true),
              'notEmpty' => __('The password must not be empty.', true)
          )
      ));
      echo $form->input('password', array(
          'label' => __('Repeat Password', true)
      ));
      

      【讨论】:

      • 哦,我不知道您可以在表单助手中指定错误消息作为选项,这大大简化了事情!
      • 它在食谱中 - book.cakephp.org/view/1401/options-error。请注意,“confirm_password”和“password”字段的标签已切换。
      【解决方案3】:

      然后您可以通过 $this->modelName->invalidFields() 访问字段和消息,这将返回未通过验证的字段和您为它们设置的消息...

      我的意思是在控制器中......

      http://book.cakephp.org/view/1182/Validating-Data-from-the-Controller

      【讨论】:

        猜你喜欢
        • 2017-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-07
        • 2012-03-24
        相关资源
        最近更新 更多