Symfony2 AJAX validation

ajax, jquery, jquery-validate, symfony, validation

Solution

You can send the serialized form via AJAX and return the rendered form from the server side if it contains validation errors.

$.post($form.attr('action'), $form.serialize(), function (response) {
    if (response.success) {
        // do something on success
    } else {
        $form.replaceWith(response.form);
    }
});

On the server side you check if it's an AJAX request and return `response` in the JSON format.

Problem

I'm using Symfony2 components and Doctrine. Validation works great on server side (constraints in Entities etc.) What I want is to validate form (my own custom built without using Symfony Form component) via AJAX. Is there any way to use Validation component to validate fields using AJAX? Or I should use jQuery validation plugin? Which seems not logical since then there will be 2 different validations used.

Original source