为什么这个Yii验证器不起作用?

Hi I'm trying to make my own Form validator just like the authenticate method in the LoginForm that is generated on the default configs.

    public function rules()
    {
        return array(
                // username and password are required
                array('mnemonic, target_reg, source_reg', 'required'),
                // rememberMe needs to be a boolean
                array('target_reg_indirection, source_reg_indirection', 'boolean'),

                array('mnemonic','foo'),

        );
    }

and here is the validator method:

    public function foo($attribute,$params){
          $this->addError('mnemonic', 'there was an error, you foo!');
      }    

it just doesn't work for me... notice how I added a rule that should not-work everytime. I just made it so I could see how it worked. But I never get to see the error message in my view. The default validators (like the one that checks for required fields) work.

Any ideas?

Because not all validators map to a client-side validator.

IN addition to that, I've created the code to handle ajax form validation and I've enabled ajaxValidation in my CActiveForm.

So now all works great. The validator I've created is working via ajax validation.

I had the same problem, in my case wasn't to enabled ajaxValidation, but the "safe" validator and a mix of other things.

Here I post some reading that helped me to solve the problem. Hope this will help others with the same problem.

  1. I read a little bit about "safe" validator. (http://www.yiiframework.com/wiki/161/understanding-safe-validation-rules/)
  2. I Understand the difference between AjaxValidation and ClientValidation. (http://www.yiiframework.com/doc/api/1.1/CActiveForm)
  3. I created my own validator class. (search "custom validation yii" on google).