简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM