phpyiiyii-validation

Condtitional Validation rules in Yii


I have a problem in custom validation rules in Yii. I have some fields like day1s, day1e, day2s, day2e etc. I want to check whether these attributes have the same value stored in my db or not before creating a new record. And also check for different userid. If a value already exists I want to generate an error and prompt the user to change the value. I figured to do something like this:

 array('day1s, day1e, day2s etc','unique','message'=>'day1s is already exist, please change'),

This kinda work but I want to modify it. The default value of this is "00:00" and if I put this rule then every time I will go and create a new record it generates the error, except the first time. I want to ignore that when day1s==00:00. And also if the user changes(in my occasion the user is SchoolID). I'm having trouble wrapping around my head on how to do that. Thanks in advance!


Solution

  • Add allowEmpty in rule:

    array('day1s, day1e, day2s etc','unique','message'=>'day1s is already exist, please change', 'allowEmpty'=>true),
    

    In controller before validation add this code:

    if($model->day1s == "00:00") {
       $model->day1s = '';
    }