1

I have a simple form and I use jquery UI date-picker for birthday input.

and in the server side I use symfony validation with Date validation.

the problem is that the validation accepts only inputs with this format: YYYY-MM-DD

but in other locals like in UK the format is: DD/MM/YYYY

and the server gives error on it.

Do you have any suggestions?

I use the symfony form object with the inside validation, so I would prefer not to force a format change manually.

1 Answer 1

1

As you can read from the documentation "date", the constraint dates performs validation only format "YYYY-MM-DD".

So you have two choices:

one (the simplest and most logical) is to set the format manually in the form field "format":

$builder->add('date', 'date', array(
    'widget' => 'single_text',
    'format' => 'dd-MM-yyyy',
));

in this way will be the form itself to reverse the date and pass the correct format to the validator (and the database).

The other is to create a constraint callback "callback", and creating your own logic for validation.

There would also be another way, even if it is inserted in the bonds of strings, and it is the constraint regex "regex".

In this way you will need to create a regular expression very precise (and complex).

For a problem as simple as the first solution is the most suitable!

EDIT:

jquery ui datepicker locks to an input type text, you do not need to create it manually.

As I wrote in the example, if you use the 'widget' => 'single_text' will produce an input type text!

You can set up a class to the date field in order to hook into that with jquery.

A short example:

form:

$builder->add('date', 'date', array(
    'widget' => 'single_text',
    'format' => 'dd-MM-yyyy',
    'attr'   =>  array(
            'class'   => 'your_class'),
));

javascript:

$(function() {
    $( ".your_class" ).datepicker();
});
Sign up to request clarification or add additional context in comments.

1 Comment

and if I don't use the symfony form date format. I use jquery datepicker.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.