I need to add some rule for my model, that checks if value (id from other table) was assigned or not (it must be unique for each record). I’d like to display in message for this rule value, but not to show id (selected with dropdown), but it’s value from the other column.
Tables:
Users - user_id, login, pass
Persons - p_id, user_id, name, surname
I want to show message, that chosen LOGIN is assigned to the other PERSON.
Hi, i have done check unique email using add Error so please see it I hope it’s some help.
call a checkEmail on rules function and check in DB
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('service_id, review_date, rating, review', 'required'),
array('email', 'email'),
array('email', 'checkEmail','on'=>'guestUser'),
array('review', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('service_id, user_id,email', 'safe', 'on'=>'search'),
);
}
/**
* @param string the name of the attribute to be validated
* @param array options specified in the validation rule
*/
public function checkEmail($attribute,$params)
{
$models = ServiceReviews::model()->findAllByAttributes(array('email' =>$this->email,'service_id'=>$this->service_id));
if(count($model)>0){
$this->addError($attribute, 'You have already submitted review for this item');
}
}