Skip to content

[Form] Unwanted Validation on DateTime Type #7182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
raphpar opened this issue Feb 25, 2013 · 10 comments
Closed

[Form] Unwanted Validation on DateTime Type #7182

raphpar opened this issue Feb 25, 2013 · 10 comments
Labels

Comments

@raphpar
Copy link

raphpar commented Feb 25, 2013

Hi there,

I have notice that for a datetime Type, an automatic validation is done after form submiting.
I splitted my field like this:

$builder->add('matchDate', 'datetime', array(                   
    'required' => false, 
    'invalid_message' => 'Hello',
    'date_format' => 'dd/MM/yyyy', 
    'date_widget' => 'single_text', 
    'time_widget' => 'choice'
))                  

If I put for example "02/35/2013" in my matchDate.date field, I get an error on the matchDate field with "This value is not valid".

I looked in Symfony\Component\Validator\Constraints\Date.php, Symfony\Component\Validator\Constraints\Time.php, Symfony\Component\Validator\Constraints\DateTime.php and anywhere I could find this error message.

On my entity I have no validation constraints set. Did anyone notice the same behavior on this application ?
In fact, it's not a problem that the validation is done, it's more the unposibility to change the error message even in setting the 'invalid_message' option.

Thanks in advance for you help.

By the way, I'm actualy working on 2.2.0BETA1

@lazyhammer
Copy link
Contributor

Turns out that after ac69394 invalid_message and invalid_message_parameters are no longer being propagated to datetime's children. What was the rationale for removing this, @bschussek?

@alpacaaa
Copy link

alpacaaa commented May 8, 2013

This is really annoying...

@Tobion
Copy link
Contributor

Tobion commented May 8, 2013

The form framework itself also applies some validation. In this case it tries to transform the input to a DateTime object in a Transformer which fails. Such errors only get a generic "invalid" error message which cannot be customized. This is a known limitation.

@kwisatz
Copy link

kwisatz commented Nov 13, 2013

Subscribing. I do have the same issue with a datetime field not validating whatever I do, with the same generic failure message.
@Tobion, how could I trace the failing DateTime transformation? I'm using symfony forms in version 2.3.6 on Silex.
Any known workarounds other than using a text or self-defined widget?

Edit: I've just found @bschussek's comment on Stackoverflow regarding this (http://stackoverflow.com/a/18367705/2373960) and I guess I've figured out where exactly my validation error stems from.

@Tobion
Copy link
Contributor

Tobion commented Nov 13, 2013

Using a custom form type is the only solution that comes to my mind.

@kwisatz
Copy link

kwisatz commented Nov 13, 2013

Quick write up of what I did to get this working. Might not be the best solution, but it works.
In the end, I guess my issue was more a pebkac problem (i.e. me) than symfony forms.

The default widget with datetime is the 'choice' widget, which returns an array of values (year, month, day, ...) instead of a string. However, my widget returned a string, which made the Transformer fail with "Compound forms expect an array or NULL on submission." and thus the "This value is not valid" error message.

So what I did was to configure the datetime field to use single_text as its widget, with my custom widget overriding this to render a bootstrap enabled datetimepicker. Before that, I had overwritten the default datetimewidget, thus the choice widget, which of course wouldn't work.

@webmozart
Copy link
Contributor

Fixed in #9453.

@alpacaaa
Copy link

Praise the Lord!

@Jaddyboy
Copy link

You need to set the widget to single_text:
->add('created_on', 'datetime', array(
'input' => 'datetime',
'widget' => 'single_text'
))

@xabbuh
Copy link
Member

xabbuh commented Jul 26, 2016

@chaos-drone Please check if the issue persist with the latest 2.8 patch release (2.8.8 at this time) and open a new issue then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants