Symfony2 AJAX验证

Jur*_*ers 8 validation ajax jquery jquery-validate symfony

我正在使用Symfony2组件和Doctrine.验证在服务器端很有用(实体等中的约束)我想要的是通过AJAX验证表单(我自己定制而不使用Symfony Form组件).

有没有办法使用验证组件来使用AJAX验证字段?

或者我应该使用jQuery验证插件?从那时起,这似乎不合逻辑,将使用2种不同的验证.

Eln*_*mov 9

您可以通过AJAX发送序列化表单,如果包含验证错误,则从服务器端返回呈现的表单.

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

在服务器端,您检查它是否是AJAX请求并response以JSON格式返回.