Permalink
Browse files

fix for GRAILS-4446

  • Loading branch information...
1 parent d9f637f commit 05fb1c030776c33e1d53ba627895691fddb39e39 @graemerocher graemerocher committed Jul 22, 2009
@@ -21,6 +21,7 @@
import org.apache.commons.logging.LogFactory;
import org.codehaus.groovy.grails.exceptions.GrailsDomainException;
import org.codehaus.groovy.grails.validation.ConstrainedProperty;
+import org.codehaus.groovy.grails.plugins.DomainClassGrailsPlugin;
import org.springframework.validation.Validator;
import java.beans.IntrospectionException;
@@ -512,6 +513,7 @@ public ComponentDomainClass(Class type) {
this.properties = createDomainClassProperties(this,descriptors);
try {
this.constraints = GrailsDomainConfigurationUtil.evaluateConstraints(getReference().getWrappedInstance(), properties);
+ DomainClassGrailsPlugin.registerConstraintsProperty(getMetaClass(), this);
} catch (IntrospectionException e) {
LOG.error("Error reading embedded component ["+getClazz()+"] constraints: " +e .getMessage(), e);
}
@@ -93,13 +93,7 @@ class DomainClassGrailsPlugin {
def metaClass = dc.metaClass
def domainClass = dc
- metaClass.'static'.getConstraints = {->
- domainClass.constrainedProperties
- }
-
- metaClass.getConstraints = {->
- domainClass.constrainedProperties
- }
+ registerConstraintsProperty(metaClass, domainClass)
metaClass.hasErrors = {-> delegate.errors?.hasErrors() }
@@ -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) {
def metaClass = dc.metaClass
@@ -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.