I’m using the following widget to allow a user to input a date
$this->widgetSchema['first_registration'] = new sfWidgetFormDate(array(
'format' => '%month%/%year%',
'years' => range(date('Y', time()) - 15, date('Y', time()) + 15)
));
The default format for a date using this widget is %month%/%day%/%year% and so that’s what the default validator for this widget checks for. However, I’ve attempted to alter the validator…
$this->validatorSchema['first_registration'] = new sfValidatorDate(array(
'date_format' => '%month%/%year%',
'with_time' => false
));
This isn’t working and I’m still getting an invalid error. Does anybody know why?
Your widget and validator still include the day field.
All your format argument does is to prevent the day from displaying – it still exists and is being validated behind the scenes. I expect that this is where the problem is occurring.
I’d recommend the easiest fix is to default the day to 1 by over-riding the bind method on the form in question. Not sure how you are doing things, but when I am working with month/year dates I often do this in the database, as it allows native date/time formats to work correctly – same argument applies to symfony forms.
Additionally, the date_format option for sfValidatorDate takes a regular expression as its argument, not a format as used in sfWidgetFormDate.