Announcement Announcement Module
No announcement yet.
Validation fun Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Validation fun

    I am currently working on porting a complex form entry to grails from java/Spring MVC. I've run into a couple issues with validation - one which I want to share my solution (in case it can help or you have a better one) and the other because it is a mystery.

    1. validation of object graphs in Grails STOPS at first child with error!!
    (I have come across old blogs that state this and it is what I am seeing but then I also came across material that states that a deep nested validation is the default so I am left confused on this subject. But this is how I fixed it in my code (using ideas gleaned here and there). I did not get any validation errors past the first child until I added this custom validator.

    Class parent

    static hasMany = [children:Child]

    static constraints = {
    children validator: {
    it?.each {it?.validate()}
    it?.every{! it.hasErrors()}

    Seems to work fine and I get ALL children validated.

    2. WEIRD issue - I have a field named title. 'title' will validate but I can't retrieve the errors using title for the field value. hasErrors will return true for filed='title' but renderErrors will not retrieve the message. Experimenting I renamed the field to 'titlex' and it works fine! Of course my message can't read 'titlex is required' !!

    thanks for any comments, tips in advance
    Last edited by cdaoust; Apr 7th, 2011, 10:32 AM. Reason: more details / findings

  • #2
    Please raise both as issues. As for the confusion, that stems from the fact the validation stops on the first child that has an issue. The validation should cascade and aggregate all errors.

    I think the 'title' issue will need a reproducible example, because I have never noticed the problem with the many library apps I've created where a Book domain class has a 'title' field. Also, test with the other tags such as <g:fieldError bean="${book}" field="title"/> and <g:fieldValue bean="..." field="..."/>. And what happens if you print out the 'errors' property?