user1234 user1234 - 2 months ago 15
PHP Question

How to apply validation?

Hi have a table in which i have many fields but i need to put validation in fields.

1)user_mobile
2)admin_mobile
3)user_email
4)admin_email

all these are saved in same database i want to apply unique validation on email and mobile. Such that no user and admin has same mobile or email. I used following rule but it don't solve my problem. Please help. Any help is appreciated.

public function rules()
{
array('user_mobile,admin_mobile,user_email,admin_email','unique',
'message'=>'{attribute} already exist')

}


It works only for user's and admin. but i want no new user or admin has same mobile no and not same email.
but this validation not matching admin_email with user_email and admin_mobile with user_mobile.

Answer

First you have to add one condition for check if you have add validation in controller.

public function actionname(){
       $model = new modelname; // model name enter here 
       $model->setattributes($_post['modelname']) // set record into model 
  if($model->validate()){
       $this->redirect('index);
     }else{ ... }
Comments