php - Required only, if other field has special value? -


is possible this. using yii 1.1

 public function rules()         {          return array(                 array('user_firstname, user_lastname, user_username, user_password,user_mobile,user_email', 'required','on'=>'createuser'),              if ($this->user_role > 2 )                {                   array('user_special_permission, 'required'),                         }                  array('user_email, user_username, user_password', 'length', 'max'=>255),                 array('user_active, user_deleted', 'length', 'max'=>1),                              // following rule used search().                 // @todo please remove attributes should not searched.                 array('user_id, user_firstname, user_lastname, user_mobile, user_email, user_username, user_password, user_last_login, user_num_logins, user_num_failed_logins, user_active, user_deleted', 'safe', 'on'=>'search'),             );         } 

i want user_special_permission required if user_role >2

for yii 1.1 can implement custom validation rule in model class follows:

in rules array add:

array('user_special_permission, 'role_validation'),  

then create function role_validation:

public function role_validation($attribute, $params) {      if ($this->user_role > 2 && empty($this->user_special_permission)) {           $this->adderror('user_special_permission','special permission required.');      } } 

you can adjust attribute error message displayed, instead of user_special_permission instance user_role

see declaring validation rules more documentation.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -