DISQUS

Chris Winters : cwinters.com | Java validation framework in JavaScript?

  • Antti Vähäkotamäki · 8 months ago
    I am not sure if I understood the problem correctly but have you thought of doing the validation on the server side using a javascript interpreter like Rhino?
  • Chris Winters · 8 months ago
    Antti - Rhino or the java6 built-in scripting (a modified Rhino) is exactly what I was thinking of. I think you'd be able to abstract out the differences enough that nearly all your validation code could be written without caring which you were running on.

    Chris
  • dshaw · 7 months ago
    If you're using Spring, you can wire your validation in using DWR (Direct Web Remoting). Appfuse-light uses this approach ( https://appfuse-light.dev.java.net ), as does the far more obscure IWebMvc ( http://code.google.com/p/internna/ ).

    Spring and DWR:
    * http://today.java.net/pub/a/today/2007/12/02/aj...
    * http://www.codercorp.com/blog/spring/ajax-based...
    * http://internna.blogspot.com/2007/03/generic-va...

    I need to figure this out myself. I have two projects where we're using the simpler MVCJ (Jersey) as the main framework and I'm going to have to wire in some validation to our forms soon.

    An emerging option might be to wire in JSR 303 Bean Validation (nee Hibernate Validation). There's apparently a JSF extension that wires in the view layer if you're in JSF-land, but I'm personally not interested in that. ( http://www.scribd.com/doc/7283078/Hibernate-Val... )

    Ping me if you come up with something else interesting.
  • Chris Winters · 7 months ago
    I have heard of DWR and similar appraoches. But I'm interested in having the validation code execute *within the browser* on the client-side for most rules, enabling an AJAX invocation for rules that can only be executed on the server (e.g., "is this username unique?"). With Rhino/Java6 there's no reason we cannot execute that same code on the server.

    Put another way: I don't think there's any reason to tax the server for rudimentary things like length or regex checking, or any of the large number of validations you can perform given only the data on the page. True, you can use code generation to create these rules in JS. But as a former codegen zealot I am now hugely wary of any solution that depends on it :-)

    The JSR-303 stuff looks interesting as well, but I feel like it's at the wrong level -- IME users only rarely manipulate single objects at a time. I don't have anything more than that vague gut feeling right now.

    I have a few validation links around, including an early JSR-303 implementation:

    http://delicious.com/cwinters/validation
  • dshaw · 7 months ago
    Interesting, I look forward to see what you come up with.

    I too resisted relying too much on JavaScript since the Defense contracts I worked on for years had heavy requirements for backward / degradable compatibility. However, whenever I've used frameworks in the past that handle both Server and Client side validation, I've always found one or the other to be lacking, so I've become a bit skeptical. It's a good Holy Grail, but IME validation is still a PITA to nail down correctly.