Search code examples
symfonysymfony-formssymfony-2.3

Symfony2 AJAX Validation - error message formatting


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


Solution

  • 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);    
       }
    });