Own Class Validate Not Firing

Hi guy,

I create custom class validate:




class AccountNumber extends CValidator

{


    private $MIN_COUNT = 3;

    private $MAX_COUNT = 10;


    /**

     * Validates a single attribute.

     * This method should be overridden by child classes.

     * @param CModel $object the data object being validated

     * @param string $attribute the name of the attribute to be validated.

     */

    public  function validateAttribute($object, $attribute)

    {

        $this->addError($object,$attribute, 'Account Number is Invalid');

    }


    


}



in extensions folder,

and in my model in rule function:




public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

            array('account_number', 'ext.AccountNumber'),

            array('account_number', 'required'),


		);

	}



but in form error message not appear,

Could and bode help me???

Seems to be correct.

  • maybe you don’t display errors in your view?

    Does the ‘required’ rule alone (comment your custom validator rule) display the error if the account_number is empty?

  • do you have clientside validation enabled?

    If yes, you have to implement the method ‘clientValidateAttribute()’ in your validator too.

  • debug, or do a 'die(“xy”) in the validateAttribute method of your validator to see if the method is called.

Yes it apear,

I don’t want client side validation,

And debug / die() ?

I have edited above comment.

You don’t need a custom validation class, you can use a simple model method for validation too.

Did you try a validation method added to your model?

because I need validate attribute in multi model.

but I try use to in model but not work ! :(

I can Solved my problem :D

I forgotten to add $model->validate in my action :D

Thx yii