0

I'm trying to create a form that is an ajax form that will have validation on 2 fields.

I have managed to get the form working via AJAX, but I'm having trouble returning the error messages Link Here

I can determine that the form is valid using $eventForm->isValid() but I want to display errors for each of the invalid fields, preferably next to each form input/widget

Is that possible?

Thanks

1 Answer 1

1

You can render individually the label, input and errors of each field in the form:

{{ form_label(form.age) }}
{{ form_errors(form.age) }}
{{ form_widget(form.age) }}

More information in Symfony2 docs: http://symfony.com/doc/current/cookbook/form/form_customization.html

EDIT based on comments:

Getting the html from an ajax request to a controller:

$.ajax({
   type: "POST",
   url: "{{path('yourpath')}}",
   cache: "false",
   dataType: "html",
   success: function(result){
        $("#somediv").append(result);    
   }
});
Sign up to request clarification or add additional context in comments.

3 Comments

I have that, but how do I get them to be populated from the ajax response?
you can pass the rendered view as you want it to be displayed and then replace the content of a container with that: success: function(data) { $('#somediv').html(data); }
I managed to get things to work. I had to serialize() the form data as it was complaining about the CSRF token.

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.