Call Yii Validator from Model custom validate function

I have a problem with one model, that I have different validation scenarios, if user in form change one select field. As I read that there is not possible to use multiple scenarios, because of that I created one custom validation function and I want in that function call Yii Validation classes, that I use in model rules() method. How this is possible?

Sample code from model:

public function rules()


		return array(

            array('profileType', 'validateProfileTypeFields', 'on'=>'profile-update'),






    function validateProfileTypeFields(){


            //Here i want to use CRequiredValidator for model field firstName and lastName and return error message 'Empty!' if field is empty

            //I want here implement same functionallity, that would do same as

            //array('firstName, lastName', 'required', 'message'=>'Empty!') do in rules() method



        } else {

            //Here i want to use CRequiredValidator for model field companyName and return error message 'Empty!' if field is empty





You calling of custome rule is exactly fine.But you need to define a method in model like below give code…

public function validEmail($attribute,$params)



	 $criteria = new CDbCriteria();



	 $validUser = $this->find($criteria);


		$this->addError($attribute, 'please enter a valid email address.we have not got your account details from this email address !');


For more reference look at this Wikki Article

That I want to achieve is a little bit different. I don’t need to create my own custom validation rule class, I just want to call standart Yii validation rule classes from model custom validation function, in this case from validateProfileTypeFields()

You can do it in two ways:

  1. prepare different scenarios for every profile type and set this scenario in action just before calling ‘validate’ or ‘save’:

$model->attributes = $_POST['Profile'];

$model->setScenario( $model->profileType == 1 ? 'update-profile-1' : 'update-profile-2' );


in this case you just provide different validation rules for different profile types.

  1. another approach is what you suggested - use standard walidators in you function but this will require more coding:

$req = new CRequiredValidator();

$req->validateAttribute( $this, 'firstName' );

$req->validateAttribute( $this, 'lastName' );


and so on… you could find implementation of rules object creation and adopt this to your needs to use array notation when creating rules, but the core is always to create validator object, set needed attributes (configuration) and call validateAttribute…

First solution isn’t good for me, because I have 20 fields in form and there would be only one change in different scenarious what means a lot of repeating code.

On second solution there is not such function validatateAttribute for this class.

Could You please post here working example?

ok… this is a protected method.

you have to use it this way (using factory):

$req = CValidator::createValidator( 'required', $this, array('firstName','lastName') );

$req->validate( $this );

for other configuration param you can add 4th param to ‘createValidator’ as an asso-array.

$req->validate( $this ); always return NULL

does anybody know how to validate in this situation? Every time I validate I get NULL in response

That’s expected. CValidator::validate() doesn’t have any return value. It adds error messages (if any) directly to the model instance.



after validating some invalid attributes.