Reputation: 389
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'=>'>'],
];
}
Upvotes: 6
Views: 17198
Reputation: 969
You can use default compare validation for this issue
['fromDate', 'date', 'timestampAttribute' => 'fromDate'],
['toDate', 'date','timestampAttribute' => 'toDate'],
['fromDate', 'compare', 'compareAttribute'=> 'toDate', 'operator' => '<',
'enableClientValidation' =>false],
Upvotes: 9
Reputation: 389
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
Upvotes: 23