Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 Date compare validation

I have model rules() like below, but it is not working. It is showing error message always.

public function rules()
{
    return [
        [['start_date','end_date'], 'date', 'format' => 'php:F d Y'],
        ['start_date','compare','compareAttribute'=>'end_date','operator'=>'<'],
        ['end_date','compare','compareAttribute'=>'start_date','operator'=>'>'],
    ];
}
like image 634
shreenu konapala Avatar asked Aug 31 '15 09:08

shreenu konapala


1 Answers

public function rules()
{
    return [
        [['start_date','end_date'], 'date', 'format' => 'php:F d Y'],
        ['start_date','validateDates'],
    ];
}


public function validateDates(){
    if(strtotime($this->end_date) <= strtotime($this->start_date)){
        $this->addError('start_date','Please give correct Start and End dates');
        $this->addError('end_date','Please give correct Start and End dates');
    }
}

you should write Client-Side Validation. http://www.yiiframework.com/doc-2.0/guide-input-validation.html#client-side-validation

like image 81
shreenu konapala Avatar answered Dec 13 '22 15:12

shreenu konapala