Announcement Announcement Module
Collapse
No announcement yet.
Beginning With Validation MVC - decision doubt Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Beginning With Validation MVC - decision doubt

    Hello guys

    i am learning about Spring mvc (rookie in this topic only), reading a book of Apress called
    Expert Spring MVC and Web Flows

    first i used to work with struts and its validations (validation.xml and my own validations)

    well in the mentionated book, i saw that if the chapter (about validation) show 10 examples 1-2 is using the
    org.springframework.validation and the others use VaLang, so 10% against 90%,
    the chapter lack in my opinion of consistence of examples,
    so in a first glance seems that the book give to Valang how a better choice than validation.

    but in the documentation of Spring use of course validation (chapter 5)
    what type of validation i should use???, and why??, please share your knowledge

    Regards
    thanks for advanced

  • #2
    I hope you find the book useful

    Anyways, the Spring validator is a very simple implementation agnostic strategy which enables you to validate your domain objects. There are a number of implementations, some of them are adapters or bridges to allow you to use external toolkits, like valang.

    If you are doing simple "check username isn't null, make sure age is > 0" etc. then taking advantage of a re-usable validation framework like valang or jakarta commons validator makes sense. OTOH if you need to do business specific validation (check the username isn't null) then you will need to write code.

    For what it is worth, for the hundreds of validators I have written, only one of them has used a 3rd party; commons validator, all the rest were hand rolled Going forward I would probably have a more even mix.

    Remember you can mix and match them as well so you could have both a valang validator and a hand rolled validator to validate the same object. Up to you

    You might also be interested in http://blog.interface21.com/main/200...my-first-post/

    Comment


    • #3
      Hello yatesco

      thanks for the reply,

      about the book, well i am starting read the chapter 10 ,
      and my unique critic could be for chapter 9 validation, lazy chapter. but the book
      was a good adquisition (nothing is perfect )

      well about validation, i thought that was enough with validation
      like the example of the chapter 5 of Spring Documentation

      but suppose that class Person have 2 more properties type Date called borndate and deaddate (sorry for my grammar)
      its obvious that 2 validation could be, if its ( null or blank ) and format for each variable of course

      but what about of ranges???, its silly that deathdate could be minor that borndate

      so in if in the validation class

      Code:
      public void validate(Object obj, Errors e) {
              ValidationUtils.rejectIfEmpty(e, "name", "name.empty");
              Person p = (Person) obj;
              if (p.getAge() < 0) {
                  e.rejectValue("age", "negativevalue");
              } else if (p.getAge() > 110) {
                  e.rejectValue("age", "too.darn.old");
              }
      	if(staticvariabletoglobalclassvalidation.validaterangedates(borndate,deathdate)){ // <--- used by other class validator's
      		e.rejectValue("borndate", "it imposible live after death");
      	}
          }
      }
      of course that validaterangedates was made by hand by myself, and other validation methods, using special validations with regex, (check telephones,postal,code etc etc) and used by other class validator's

      my point is that i have already written these clases for validation in a work with struts (used how utils class), so i want recicle these clases for Spring mvc, i hope you see my point, (maybe this is your point when you talk about mix )

      in my honest opinion i can work in peace with ValidationUtils and Errors using my clases validation
      if this logic or way is correct.

      is this really your way???, i am little confuse

      regards
      thanks for advanced

      DrPompeii

      Comment


      • #4
        I am not really sure what you are asking

        Is it OK to hand roll your own validation; sure. What you have looks fine

        Comment


        • #5
          Hello yatesco


          Originally posted by yatesco View Post
          Is it OK to hand roll your own validation; sure. What you have looks fine
          if is fine the logic and code that i written, for the dates, thanks, that was my principal doubt

          Originally posted by yatesco View Post
          I am not really sure what you are asking
          was about of the logic, if it is the correct way that you use when you make your own validations

          regards and thanks for the reply

          Comment

          Working...
          X