Denis Felst
Denis Felst

Reputation: 57

How to set Date Range in Symfony DateType with single_text widget

I want to create a date input with the widget => single_text type, but I only want the user to be able to select/type years between 2000 and 2099. With widget => choice this works fine, but with single_text I can write the number I want despite clearly having the range attribute set. Is there a quick way to accomplish this, or do I have to create some other validation? If so, how?

this is how it looks like with single_text

My code:

... ['respiteDate', DateType::class, [
               'required' => false,
               'widget' => 'single_text',
               'years'  =>  range(2000,2099)
               ],
           ], ...

Thanks in advance.

Upvotes: 0

Views: 2138

Answers (1)

Jovan Perovic
Jovan Perovic

Reputation: 20193

What you have there is a screenshot of Chrome-specific datepicker (if not mistaken). Different browsers do it in a different way and when it comes to <input type="date"/> (MDN link) their is no way to specific which years in particular are to be permitted (contrary to choice variant).

According to standard, there is a way to specify min and max date, which you may find suitable enough - you just need to specify min="2000-01-01 and max="2099-12-31".

Interestingly enough, DateType does not seem to support min and max attributes, so be sure to include server-side validation...

Hope this helps...

Upvotes: 1

Related Questions