3 Replies Latest reply on Mar 2, 2012 8:19 AM by jbalunas

    how to avoid duplicating validation rules


      Hi AeroGear folks,


      I'm curious to hear what others think about this.


      In the AeroGear kitchensink archetype, the validation rules for the fields are written directly in index.html, as well as in Member.java.



      <!-- For now mapping bean validation constraints from server side model is a manual task -->
                          <form name="reg" id="reg" data-ajax="false">
                                  <legend>Register a user:</legend>
                                      <label for="name">Name:</label>
                                      <input type="text" name="name" id="name" placeholder="Your Name" required autofocus/>
                                      <label for="email">Email:</label>
                                      <input type="email" name="email" id="email" placeholder="Your Email" required/>
                                      <label for="phoneNumber">Phone ${symbol_pound}:</label>
                                      <input type="tel" name="phoneNumber" id="phoneNumber" pattern="[0-9]{10,12}" placeholder="Your Phone ${symbol_pound}" required/>
                                  <div id="formMsgs"></div>
                                  <div data-role="controlgroup" data-type="horizontal">
                                      <input type="submit" name="register" id="register" value="Register"/>
                                      <input type="button" name="cancel" id="cancel" value="Cancel"/>





         @Size(min = 1, max = 25, message = "1-25 letters and spaces")
         @Pattern(regexp = "[A-Za-z ]*", message = "Only letters and spaces")
         private String name;
         @Email(message = "Invalid format")
         private String email;
         @Size(min = 10, max = 12, message = "10-12 Numbers")
         @Digits(fraction = 0, integer = 12, message = "Not valid")
         @Column(name = "phone_number")
         private String phoneNumber;



      And of course, as the comment implies, it'd be nice to not have to copy the validation rules manually (like the phone number regex).


      Has anyone done tried to tackle this problem? 

        • 1. Re: how to avoid duplicating validation rules

          This is definitely one of those area's that is a little clunky - I mean we just got Bean Validation, and now we need to hardcode the rules in the html :-(


          We've been thinking about the problem, but have not had time to focus on it yet.  That is likely to change soon with M2 being released.


          Most of what I've been thinking around this is form generation with metawidget.  Richard just posted a blog on some work he's done to get metawidget creating these Metawidget and AeroGear.  It is just a proof of concept for now.  Even within metawidget there is forge gen, build time, runtime approaches to consider.  I think the holy grail here would be runtime generation of the inputs based on a defined API/Endpoint interface.  So we're not shipping HTML tags over the wire, just metadata. 


          There are other approaches as well like using a lightweight MVC to help construct the UI.  We could probably use torquebox/ruby for that, or something new.


          What are your thoughts around this?  Have any suggestions or ideas here?

          • 2. Re: how to avoid duplicating validation rules



            Obviously I'm a big believer in "runtime generation of the inputs based on a defined API", and I look forward to seeing what we can do with Metawidget there.


            However you raise an interesting point when you say "not shipping HTML tags over the wire, just metadata". When working on the Metawidget/AeroGear proof of concept, it became apparent that JavaScript/JQuery/JSON are somewhat weak at mapping and converting complex types. An open question for me, before we start on our runtime version, will be to pick one of two approaches:


            1. Send the metadata over the wire, and process it client-side. This is much 'purer' but I worry we may become stuck when dealing with complex types. For example, for the proof of concept I had to introduce a custom ObjectMapper and shoehorn a 'toString' field in there. Because otherwise JavaScript cannot call Java's toString on the Object. Populating tables is similarly clunky.


            2. Construct the HTML server-side and send it back. This is less 'pure' but may be better in terms of a) ability to convert complex types and b) browser compatibility (because the client-side JS would be much simpler).


            If you could keep this in mind while looking at the proof of concept, that'd be very helpful. Note the GWT version of Metawidget *does* do everything client-side, but then again GWT brings a raft of Java-like functionality into JavaScript. It may not be practical to replicate this for the JQuery/JSON approach.





            • 3. Re: how to avoid duplicating validation rules

              You bring up some valid concerns, and it is something we'll have to figure out.  JAX-RS in general can handle fairly complex data-structures, but I agree if it gets to complex it could be a big problem. I'll keep this in mind when reviewing the PoC.


              Just thinking out loud here - perhaps something written in gwt/errai and compiled to a static runtime JS library could help here.  I know the errai guys have talked about something like this before.  I also know that GWT has a hard time being dynamic once its been compiled.  This would not be my first choice, but it is something to think about.


              All of these approaches have pros/cons and I'd like to hear from more people about this.  Any other thoughts?  I know there must be :-)