Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 AJAX validation

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.

like image 863
Juris Vaiders Avatar asked Oct 18 '12 06:10

Juris Vaiders


1 Answers

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.

like image 137
Elnur Abdurrakhimov Avatar answered Nov 05 '22 05:11

Elnur Abdurrakhimov