Marks An Attribute Unsafe For Mass Assignment.

Hi,

I have a special custom validator (without class, only function) in model and I want to mark an attribute as unsafe. I tried with below code but the unsafe validator does not mark it as unsafe. with scenarios works properly (and may should follow this approach) but I have already wrote a lot of code and adding scenarios for each line may make my website unstable…


 public function myCustomValidatorCondition($attribute, $params) {

        if (...some condition...) {

             $valid = CValidator::createValidator( 'boolean', $this, array($attribute));

             $valid->validate($this);

        } else {

             $valid = CValidator::createValidator( 'unsafe', $this, array($attribute));

             $valid->validate($this);

        }

    }

I also noted that each validator has $safe variable to mark (?) the attribute as unsafe. how can I use this for τhe above purpose?

Anyone please ?

After of one year I face the same problem again

I solved in a different way


 public function beforeValidate() {

     if (parent::beforeValidate()) {

 

         if (...some condition...) {

             $validator = CValidator::createValidator( 'boolean', $this, array('my_attribute'));

             $this->validatorList->add($validator);

        } else {

             $validator = CValidator::createValidator( 'unsafe', $this, array('my_attribute'));

             $this->validatorList->add($validator);

        }

       return true;

    }

  return false;

}