Actually the situation is as follows:

There is a model in which the following is spelled out (did by example):

public function rules() { return array( array('begin_at', 'uniqueBeginAndEnd', 'message'=>'Π‘Ρ€ΠΎΠΊΠΈ провСдСния с Ρ‚Π°ΠΊΠΈΠΌΠΈ условиями ΡƒΠΆΠ΅ Π·Π°Π΄Π°Π½Ρ‹'), ); } public function uniqueBeginAndEnd($attribute,$params=array()) { if(!$this->hasErrors()) { $params['criteria']=array( 'condition'=>'courses_id=:courses_id AND end_at=:end_at', 'params'=>array(':courses_id'=>(int)$_GET['id'], ':end_at'=>$this->end_at), ); $validator=CValidator::createValidator('unique',$this,$attribute,$params); $validator->validate($this,array($attribute)); } } 

Those. If I try to create a new or edit an existing record, and an entry with such dates is already in the table, I will get the message 'Dates for such conditions have already been set.' So here's the problem: validation works on adding a new record, but it doesn't work on editing (i.e. update passes anyway).

What is added that editing is made standard $ model-> save ()

PS I apologize in advance if I explained it is not quite clear.

  • Add to sql ignoring the current record and all - VasyOk

2 answers 2

I would do something like this.

 public function rules() { return array( array( 'courses_id, end_at', 'unique', 'className' => 'Π½Π°Π·Π²Π°Π½ΠΈΠ΅ ΠΌΠΎΠ΄Π΅Π»ΠΈ Ρ‡Π΅Ρ€Π΅Π· ΠΊΠΎΡ‚ΠΎΡ€ΡƒΡŽ ΠΈΡ‰Π΅ΠΌ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅', 'attributes' => array('ΠΏΠ΅Ρ€Π²ΠΎΠ²Ρ‹ΠΉ Π°Ρ‚Ρ€ΠΈΠ±ΡƒΡ‚', 'Π²Ρ‚ΠΎΡ€ΠΎΠΉ Π°Ρ‚Ρ€ΠΈΠ±ΡƒΡ‚'), 'message' => 'Π‘Ρ€ΠΎΠΊΠΈ провСдСния с Ρ‚Π°ΠΊΠΈΠΌΠΈ условиями ΡƒΠΆΠ΅ Π·Π°Π΄Π°Π½Ρ‹'), array('courses_id, end_at', 'required'), array('courses_id, end_at', 'numerical'), ); } 

UPDATE
And in this case it is worth checking what was transmitted from the user, for this we need the last two rules.
I assumed that end_at is a number. If it is a date, then the date validator is needed.

UPDATE 2
Based on your comment: There is already a similar question with the accepted answer on the hash code (it is easy to search through Yandex for the query "check for uniqueness")
In short: you need to specify in the rule

 'on'=>'add' //сработаСт ΠΏΡ€ΠΈ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ сцСнария добавлСния записи 'on'=>'edit' //ΠΏΡ€ΠΈ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠΈ записи 
  • If you do this, then courses_id and end_at will be checked for uniqueness separately. And I need their combination to be unique - arivlaver
  • So I did it for this example))) and now there was such a glitch - arivlaver
  • In short, I do not know where to write, but it may be useful to someone. I cannot answer my own questions yet. I found the simplest solution in my opinion (do not think much) yiihaa.com/… . Everything works great. Only in the parameter 'with' => 'attribute 1, attribute 2, ...' specify everything without spaces - arivlaver

I am doing a bit different validation, different from the standard. Here is an example of product name validation:

 public function rules() { return array( array('name', 'filter', 'filter' => 'strip_tags'), array('name', 'filter', 'filter' => 'trim'), array('name', 'validItemName') ); } public function validItemName($attribute,$params=array()) { $pattern = "/[\!\?\"\'\+\_\/\|]/"; preg_match($pattern, $this->name, $matches); if($matches){ $this->addError('name', 'НСдопустимый символ '.$matches[0]); } } 

Maybe you should add your own error through $ this-> addError ()