Reputation: 1231
In symfony2 form creator I have a simple date field:
->add('start_date', 'date', array(
'html5' => false,
'widget' => 'single_text',
))
This does the job perfectly, however i dont want to be able to select an old date, for example if today is 2015-11-19 I dont want to be able to select 2015-11-18. Itt should be grayed out or something.
Is there a default option for something like this? If not, what could be the best approach to do what I want?
Upvotes: 1
Views: 1176
Reputation: 1
the problem is that DateType does not work with dates, but with separate lists of year, month and day values (which you can pass to the builder if you want, see http://symfony.com/doc/current/reference/forms/types/date.html). If you want the type to be aware of "dates after today" you will need to build a custom form type, it's not hard at all: http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html.
You can extend Symfony\Component\Form\Extension\Core\Type\DateType and overwrite setDefaultOptions:
class MyDateType extends DateType {
public function setDefaultOptions(OptionsResolverInterface $resolver){
parent::setDefaultOptions($resolver);
//Insert here your logic to create valid values for year, month, day
$my_list_of_years = .....;
$my_list_of_months = .....;
$my_list_of_days = .....;
$resolver->setDefaults([
'years' => $my_list_of_years,
'months' => $my_list_of_months,
'days' => $my_list_of_days
]);
}
}
Upvotes: 0
Reputation: 3135
You can achieve it with range
in specific option.
Just for example (only the day before in this case):
$builder->add('start_date','date', array(
'days' => range(date('d') -1, date('d')),
));
Upvotes: 2