Sonata Date Range

I have a little problem. I am working on a small project using Sonata, but I'm trying to figure out how to set the date range that a person can choose, for example, from January 1, 2000 to December 31, 2020. Can someone tell me where I can find it in the Sonata documentation or show me how to do this? Because I look a little, and I have nothing to completely customize the choice of dates.

Thank you in advance

Edit:

In the symfony documentation I found this

'days' => range(1,31) 

Here: http://symfony.com/doc/current/reference/forms/types/date.html

But I can’t find anything in the sonata doctor. And it doesn’t work like on Symfony: /

+5
source share
1 answer

I assume that you are using the sonata_type_date_picker field in the sonata. The documentation is here . Then your case can be implemented as follows:

 ->add('userDate', 'sonata_type_date_picker', [ 'dp_min_date' => 'Jan 1, 2000', //todo: date format here depends on your setup. Basicly it the same format, you see in text field after you selected data in datepicker. 'dp_max_date' => 'Dec 31, 2020', ]); 

Alternatively, you can choose a date selector with three drop-down menus - this is the standard Symfony date field, works great with SonataAdmin:

 ->add('userDate', 'date', [ 'years' => range(2000, 2020), ]); 

I would also recommend adding a backend validation with the same rules (in Entity):

 /** * @var \DateTime() * * @Assert\Range( * min = "2000-01-01", * max = "2020-12-31" * ) */ protected $userDate; 
+3
source

Source: https://habr.com/ru/post/1271988/


All Articles