grails / grails-core

The Grails Web Application Framework
http://grails.org
Apache License 2.0
2.79k stars 951 forks source link

GRAILS-1623: Validation does not cascade properly for domain subclass #8443

Closed graemerocher closed 9 years ago

graemerocher commented 17 years ago

Original Reporter: jkranes Environment: Grails 0.6 Version: 0.6 Migrated From: http://jira.grails.org/browse/GRAILS-1623

Validation does not cascade properly to a related domain class if the containing class is a subclass.

Example:

class Book { String title Integer pages static belongsTo = [Author] static constraints = { pages(range: 0..100) } } class Author { String name Book book } class Novelist extends Author { }

Validation will correctly cascade from Author to Book for an instance of Author, but fails to do so for an instance of Novelist:

class ValidationTests extends GroovyTestCase {

  // Passes as expected

void testCascadeValidation() {
    def b = new Book(title:'The Definitive Guide to Grails', pages:9999) // pages violates range constraint
    def a = new Author(name:'Rocher', book:b)
    assert a.validate() == false 
}

   // Fails!

void testCascadeValidationWithSubclass() {
    def b = new Book(title:'War & Peace', pages:9999) // pages violates range constraint
    def a = new Novelist(name:'Tolstoy', book:b)
    assert a.validate() == false  // Fails -- validation passes!
}

}

graemerocher commented 16 years ago

jenslukowski said: This patch searches the hierarchy of the associated class and validates on the appropiate domain class.

graemerocher commented 15 years ago

graemerocher said: Seems to be already fixed. Wrote a test anyway http://github.com/grails/grails/commit/fe775d1a7f06dc08c99ba1a0cad942aacf47101b