admin管理员组

文章数量:1289565

I'm new to symfony 2 and i'm trying to build a standard form which should be validated by the server and by the navigator.

Server-side validation works fine with assertions like @Assert\... but i cannot find a way to get this working on client side.

After searching, i pointed out that Symfony 2 does not have a javascript library to manage this..

So my question is : What's the easiest way to do this ?

I know the possibility of using client side library like jQuery Validation plugin or something like Regula, but that would means that i have to duplicate my validations rules..

I'm new to symfony 2 and i'm trying to build a standard form which should be validated by the server and by the navigator.

Server-side validation works fine with assertions like @Assert\... but i cannot find a way to get this working on client side.

After searching, i pointed out that Symfony 2 does not have a javascript library to manage this..

So my question is : What's the easiest way to do this ?

I know the possibility of using client side library like jQuery Validation plugin or something like Regula, but that would means that i have to duplicate my validations rules..

Share Improve this question edited Dec 4, 2012 at 2:53 Vivin Paliath 95.6k42 gold badges230 silver badges301 bronze badges asked Dec 3, 2012 at 13:45 LetoLeto 2,6743 gold badges26 silver badges37 bronze badges 2
  • Hello Leto, I'm the author of Regula. I did something like what you're describing in Java, where I translated JSR-303 annotations to Regula constraints on the client-side. It might be possible to do something similar in PHP? I'm not too familiar with PHP, but does it provide a way to intercept the view before rendering? If so, you could have an interceptor that translates the @Assert into regula validation-constraints. Would take some work from your side, but it is a direction that you could go. :) – Vivin Paliath Commented Dec 4, 2012 at 2:56
  • 1 Hi, yeah i thought about it, but i'm just starting with SF2 and i have already pain with trivial things so i'm not yet good enough to do this job (and furthermore, i'm not working for myself, so it does not depend on me). That said, you did great job with regula :) – Leto Commented Dec 4, 2012 at 8:25
Add a ment  | 

2 Answers 2

Reset to default 7

Symfony is a PHP framework, ie server-side application framework. It doesn't provide client-side validation unless you use HTML5 one with required attribute and other email type for input (that is still not very well implemented in browsers).

So you have to find your own solution and try to plug it in front of Symfony2 to make it as easy as possible... or find an existing bundle like APYJsFormValidationBundle

This is new Symfony 2 bundle which provide implementation of client side validation for Symfony Types (forms) https://github./formapro/JsFormValidatorBundle

(This bundle is still being maintained and seems to have support for Symfony 4)

本文标签: How to validate Symfony 2 form on client side (javascript)Stack Overflow