Reputation: 218
How can I validate date with or without month and days at same time in Yii? I mean can I validate date like year only(2011) or whole(2011-01-02) at same time in Yii ??
Upvotes: 1
Views: 166
Reputation: 841
If you want to just validate years then just add this in model rules like this.
return array(
array('your-date-attribute','your_function')
);
And add the below function on that model.
public function your_function($attribute,$params)
{
if (!preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$this->your-date-attribute) && !preg_match("/^[0-9]{4}$/",$this->your-date-attribute))
{
$this->addError($attribute, 'Date is not valid date!');
}
}
Upvotes: 1