Skip to content

Commit

Permalink
fix for GRAILS-4446
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher committed Jul 22, 2009
1 parent d9f637f commit 05fb1c0
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.codehaus.groovy.grails.exceptions.GrailsDomainException; import org.codehaus.groovy.grails.exceptions.GrailsDomainException;
import org.codehaus.groovy.grails.validation.ConstrainedProperty; import org.codehaus.groovy.grails.validation.ConstrainedProperty;
import org.codehaus.groovy.grails.plugins.DomainClassGrailsPlugin;
import org.springframework.validation.Validator; import org.springframework.validation.Validator;


import java.beans.IntrospectionException; import java.beans.IntrospectionException;
Expand Down Expand Up @@ -512,6 +513,7 @@ public ComponentDomainClass(Class type) {
this.properties = createDomainClassProperties(this,descriptors); this.properties = createDomainClassProperties(this,descriptors);
try { try {
this.constraints = GrailsDomainConfigurationUtil.evaluateConstraints(getReference().getWrappedInstance(), properties); this.constraints = GrailsDomainConfigurationUtil.evaluateConstraints(getReference().getWrappedInstance(), properties);
DomainClassGrailsPlugin.registerConstraintsProperty(getMetaClass(), this);
} catch (IntrospectionException e) { } catch (IntrospectionException e) {
LOG.error("Error reading embedded component ["+getClazz()+"] constraints: " +e .getMessage(), e); LOG.error("Error reading embedded component ["+getClazz()+"] constraints: " +e .getMessage(), e);
} }
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -93,13 +93,7 @@ class DomainClassGrailsPlugin {
def metaClass = dc.metaClass def metaClass = dc.metaClass
def domainClass = dc def domainClass = dc


metaClass.'static'.getConstraints = {-> registerConstraintsProperty(metaClass, domainClass)
domainClass.constrainedProperties
}

metaClass.getConstraints = {->
domainClass.constrainedProperties
}


metaClass.hasErrors = {-> delegate.errors?.hasErrors() } metaClass.hasErrors = {-> delegate.errors?.hasErrors() }


Expand Down Expand Up @@ -155,6 +149,19 @@ class DomainClassGrailsPlugin {
} }
} }


/**
* Registers the constraints property for the given MetaClass and domainClass instance
*/
static def registerConstraintsProperty(MetaClass metaClass, GrailsDomainClass domainClass) {
metaClass.'static'.getConstraints = {->
domainClass.constrainedProperties
}

metaClass.getConstraints = {->
domainClass.constrainedProperties
}
}



private static addRelationshipManagementMethods(GrailsDomainClass dc) { private static addRelationshipManagementMethods(GrailsDomainClass dc) {
def metaClass = dc.metaClass def metaClass = dc.metaClass
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.codehaus.groovy.grails.web.binding

import org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerTests

/**
* @author Graeme Rocher
* @since 1.1
*/

public class BindToObjectWithEmbeddableTests extends AbstractGrailsControllerTests{

protected void onSetUp() {
gcl.parseClass('''
import grails.persistence.*
@Entity
class Person {
static embedded = ['address']
String name
int age
Address address = new Address()
}
class Address {
String street
String street2
String city
String state
String zip
static constraints = {
street2(nullable:true)
}
}
class PersonController {
def save = {
def p = new Person(params)
[person:p]
}
}
''')
}


void testBindToObjectWithEmbedded() {
def controller = ga.getControllerClass("PersonController").newInstance()

controller.params.name = "Joe"
controller.params.age= "45"
controller.params.'address.city' = 'Brighton'

def model = controller.save()

assertEquals "Joe", model.person.name
assertEquals 45, model.person.age
assertEquals "Brighton", model.person.address.city

}

}

0 comments on commit 05fb1c0

Please sign in to comment.