简体   繁体   English

Symfony2 AJAX验证

[英]Symfony2 AJAX validation

I'm using Symfony2 components and Doctrine. 我正在使用Symfony2组件和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. 验证在服务器端很有用(实体等中的约束)我想要的是通过AJAX验证表单(我自己定制而不使用Symfony Form组件)。

Is there any way to use Validation component to validate fields using AJAX? 有没有办法使用验证组件来使用AJAX验证字段?

Or I should use jQuery validation plugin? 或者我应该使用jQuery验证插件? Which seems not logical since then there will be 2 different validations used. 从那时起,这似乎不合逻辑,将使用2种不同的验证。

You can send the serialized form via AJAX and return the rendered form from the server side if it contains validation errors. 您可以通过AJAX发送序列化表单,如果包含验证错误,则从服务器端返回呈现的表单。

$.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. 在服务器端,您检查它是否是AJAX请求并以JSON格式返回response

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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