09.
Date Format & "Sanity" Validation
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
14 Comments
Is there a way to change the Symfony's date format, without showing the ugly "2016-10-10" to the user?
Yo Maksym,
Yes, you can customize it! Check out the "format" option of Date form type in docs.
Cheers!
So much better! Thank you.
Hi,
I have a similar setup where I have a value that is a DateTime object (your screen shot shows @ORM\Column(type="date")). I got everything to work, but when I submit the form the date is being added without the time. I have tried using the DateTime form type, but it returns NULL when I submit the form. Any ideas?
Yo Mike!
Hmm, is your @ORM\Column a type="date" or type="datetime"? If it's "date", change it to "datetime": our form is correctly submitting a DateTime with time, but then Doctrine is saving only the date part.
I'm not sure why the DateTimeType on the form would be returning null, however. If the above doesn't give you a hint, feel free to post some code (the important part if your entity, form, controller) and we'll see if we can spot the issue :).
Cheers!
Thanks for getting back to me.
My @ORM\Colum is type="datetime". Take a look below at my setup:
Entity:
Controller:
FormType:
Twig:
When I submit the form it will add it to the DB like this:
2016-11-30 00:00:00
It turns out that if you set your input field to disabled it will return a NULL value. If you use readonly instead it will work just fine. I did not include the disabled bit in my code above, but that is how I had my form originally. I was also using a datepicker that did not include time so I needed to use the bootstrap datetimepicker to resolve that. In the end I decided to use readonly and add today's date by default.
Good detective work Mike! The disabled thing is by default (but I could see how that could be a gotcha) - so that if you have a disabled field that already has a value, a "clever" user can't just modify it in the HTML and submit. I believe this is based on some official spec somewhere (that disabled input should be ignored, but readonly shouldn't), but I can't remember for sure :).
Glad you sorted it out!
I have set 'widget' => 'single_text', 'format' => 'dd/MM/yyyy' and it won't work. I need to use spanish format for single_text (html5 false, jquery datapicker is configured to dateFormat: 'dd/mm/yy'). It works, if I set both config to expected Symfony format, but I need to provide users dd/MM/yyyy format. Thanks in advance.
Hey Vkgroup!
Hmm. So, you are setting Symfony's format to
dd/MM/yyyyand jQuery's date picker format also todd/MM/yyyy. I think I might know the problem. I believe Symfony's date field and jQuery's date picker use different "ways" to describe the correct format. See the red warning box at the bottom of this section: http://symfony.com/doc/current/reference/forms/types/date.html#rendering-a-single-html5-textbox (it starts with "The string used by a JavaScript date picker...").For jQuery's date picker, you may need to use
yyyy/mm/dd... or something similar.Let me know if that helps! Cheers!
Hi,
I'm following this tutorial. I added :
$('.js-datepicker').datepicker();
as was mentioned in the tutorial, entered Genus data in the form, selected date and hit save. I was expecting to see a validation error like the tutorial but I got a *bigggg* explosion:
Warning: IntlDateFormatter::parse(): Date parsing failed
500 Internal Server Error - ContextErrorException
in vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToLocalizedStringTransformer.php at line 123
// to DST changes
$dateOnly = $this->isPatternDateOnly();
$timestamp = $this->getIntlDateFormatter($dateOnly)->parse($value);
if (intl_get_error_code() != 0) {
throw new TransformationFailedException(intl_get_error_message());
any idea what's going on?
thanks
Hey Mehdi,
Hm, looks like Symfony can't parse the date you passed. Try to specify the next format when initializing datepicker:
Does it work? The problem could be in Intl version, I suppose you're on Windows, right? I also wonder what Symfony version do you use? I think as much as you can do here is to update your application to the latest Symfony version. It should probably fix the problem. But maybe not, then probably you need also to upgrade Intl on your computer, but it's difficult to do on Windows as I know. So probably, the easiest way is to pass the correct date format.
Cheers!
i dont have the good DateType file my is almost empty
Hey Joram,
Most probably you opened a wrong file. Please, double check the path - it should be:
./vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/Type/DateType.php. Also, the namespace of this file should beSymfony\Component\Form\Extension\Core\Type.Cheers!
"Houston: no signs of life"
Start the conversation!