Details
-
Type:
New Feature
-
Status:
Closed
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: 1.3.3
-
Fix Version/s: 2.0 final
-
Component/s: None
-
Labels:None
-
Environment:Grails 1.3.3
sun jdk 1.6.0_21 b07
windows 7 64 bit
Description
I think it will be very useful to have a possibility for reusing specified set of constraints from domain class in command class. Besides being in line with DRY principle, it will prevent errors when one forget to update changed constraint in both classes.
To better explain what I mean, here is some sample code:
import org.codehaus.groovy.grails.validation.ConstrainedProperty; import org.codehaus.groovy.grails.validation.Constraint; class MyGrailsUtils { static void copyConstraintsFromDomainToCommand( def p_domainClass, def p_commandClass, String p_constrainedPropertyName, String p_constraintName) { Map domainClassConstraints = p_domainClass.getConstraints() ConstrainedProperty domainClassConstrainedProperty = domainClassConstraints[p_constrainedPropertyName] Constraint domainClassPropertyConstraint = domainClassConstrainedProperty.getAppliedConstraint(p_constraintName) String domainClassPropertyConstraintName = domainClassPropertyConstraint.getName() Object domainClassPropertyConstraintParameter = domainClassPropertyConstraint.constraintParameter def commandClassConstraints = p_commandClass.getConstraints() Map commandClassConstrainedProperties = commandClassConstraints.getConstrainedProperties() ConstrainedProperty commandClassConstrainedProperty = commandClassConstrainedProperties[p_constrainedPropertyName] commandClassConstrainedProperty.applyConstraint(p_constraintName, domainClassPropertyConstraintParameter) } } class User { String userId ... static constraints = { userId(blank:false, size: 3..20, unique: true) ... } ... } class UserRegistrationCommand { String userId ... static constraints = { userId(blank:false) ... // Copies size constraint of userId property from User domain class. MyGrailsUtils.copyConstraintsFromDomainToCommand(User, UserRegistrationCommand, "userId", "size") } }
Code is not tested, and I'm sure it could be written much better since I'm groovy/grails beginner, and code uses some non public method and properties. However, I hope it can illustrate the idea. Of course, it would be the best to add this feature in constraints DSL somehow.
Activity
Damir Murat
made changes -
| Field | Original Value | New Value |
|---|---|---|
| Attachment | 0001-GRAILS-6548-Allow-reuse-of-constrains-in-commands.patch [ 51950 ] | |
| Attachment | importconstraintsdemo-bug-report-30102010.zip [ 51951 ] |
Damir Murat
made changes -
| Attachment |
0001- |
Damir Murat
made changes -
| Attachment | importconstraintsdemo-bug-report-30102010.zip [ 51951 ] |
Damir Murat
made changes -
| Attachment | importconstraintsdemo-bug-report-04112010.zip [ 52034 ] | |
| Attachment | 0001-GRAILS-6584-Fixed-Allow-reuse-of-constrains-in-comma.patch [ 52033 ] |
Graeme Rocher
made changes -
| Fix Version/s | 1.4-M1 [ 16812 ] | |
| Fix Version/s | 1.3.6 [ 16730 ] |
| Repository | Branch | Revision | Date | Author/Committer | Message |
| Grails Core | master | 8313225... | 2010-11-08 07:29:59 -0800 | dmurat / graemerocher | |
| Files Changed | |||||
|
MODIFY
src/java/org/codehaus/groovy/grails/validation/AbstractConstraint.java
MODIFY src/java/org/codehaus/groovy/grails/validation/ConstrainedPropertyBuilder.java MODIFY src/java/org/codehaus/groovy/grails/validation/Constraint.java ADD src/test/org/codehaus/groovy/grails/validation/ConstrainedPropertyBuiderForCommandsTests.groovy |
| Repository | Branch | Revision | Date | Author/Committer | Message |
| Grails Core | cli-improvements | 8313225... | 2010-11-08 07:29:59 -0800 | dmurat / graemerocher | |
| Files Changed | |||||
|
MODIFY
src/java/org/codehaus/groovy/grails/validation/AbstractConstraint.java
MODIFY src/java/org/codehaus/groovy/grails/validation/ConstrainedPropertyBuilder.java MODIFY src/java/org/codehaus/groovy/grails/validation/Constraint.java ADD src/test/org/codehaus/groovy/grails/validation/ConstrainedPropertyBuiderForCommandsTests.groovy |
| Repository | Branch | Revision | Date | Author/Committer | Message |
| Grails Core | 2.0.x | 8313225... | 2010-11-08 07:29:59 -0800 | dmurat / graemerocher | |
| Files Changed | |||||
|
MODIFY
src/java/org/codehaus/groovy/grails/validation/AbstractConstraint.java
MODIFY src/java/org/codehaus/groovy/grails/validation/ConstrainedPropertyBuilder.java MODIFY src/java/org/codehaus/groovy/grails/validation/Constraint.java ADD src/test/org/codehaus/groovy/grails/validation/ConstrainedPropertyBuiderForCommandsTests.groovy |
| Repository | Branch | Revision | Date | Author/Committer | Message |
| Grails Core | hibernate4 | 8313225... | 2010-11-08 07:29:59 -0800 | dmurat / graemerocher | |
| Files Changed | |||||
|
MODIFY
src/java/org/codehaus/groovy/grails/validation/AbstractConstraint.java
MODIFY src/java/org/codehaus/groovy/grails/validation/ConstrainedPropertyBuilder.java MODIFY src/java/org/codehaus/groovy/grails/validation/Constraint.java ADD src/test/org/codehaus/groovy/grails/validation/ConstrainedPropertyBuiderForCommandsTests.groovy |
| Repository | Branch | Revision | Date | Author/Committer | Message |
| Grails Core | 2.1.x | 8313225... | 2010-11-08 07:29:59 -0800 | dmurat / graemerocher | |
| Files Changed | |||||
|
MODIFY
src/java/org/codehaus/groovy/grails/validation/AbstractConstraint.java
MODIFY src/java/org/codehaus/groovy/grails/validation/ConstrainedPropertyBuilder.java MODIFY src/java/org/codehaus/groovy/grails/validation/Constraint.java ADD src/test/org/codehaus/groovy/grails/validation/ConstrainedPropertyBuiderForCommandsTests.groovy |
| Repository | Branch | Revision | Date | Author/Committer | Message |
| Grails Core | 2.2.x | 8313225... | 2010-11-08 07:29:59 -0800 | dmurat / graemerocher | |
| Files Changed | |||||
|
MODIFY
src/java/org/codehaus/groovy/grails/validation/AbstractConstraint.java
MODIFY src/java/org/codehaus/groovy/grails/validation/ConstrainedPropertyBuilder.java MODIFY src/java/org/codehaus/groovy/grails/validation/Constraint.java ADD src/test/org/codehaus/groovy/grails/validation/ConstrainedPropertyBuiderForCommandsTests.groovy |
Graeme Rocher
made changes -
| Status | Open [ 1 ] | Closed [ 6 ] |
| Resolution | Fixed [ 1 ] |
Contegix Support
made changes -
| Project Import | Thu Mar 24 21:22:24 CDT 2011 [ 1301019744151 ] |
Burt Beckwith
made changes -
| Status | Closed [ 6 ] | Reopened [ 4 ] |
| Resolution | Fixed [ 1 ] |
Burt Beckwith
made changes -
| Fix Version/s | 1.3.6 [ 11038 ] |
Burt Beckwith
made changes -
| Status | Reopened [ 4 ] | Closed [ 6 ] |
| Resolution | Fixed [ 1 ] |
Burt Beckwith
made changes -
| Workflow | jira [ 32739 ] | Grails [ 45548 ] |
Burt Beckwith
made changes -
| Workflow | Grails [ 45548 ] | Copy of Grails [ 53004 ] |
Burt Beckwith
made changes -
| Workflow | Copy of Grails [ 53004 ] | Grails [ 60414 ] |
Burt Beckwith
made changes -
| Workflow | Grails [ 60414 ] | Grails2 [ 67992 ] |
Burt Beckwith
made changes -
| Workflow | Grails2 [ 67992 ] | jira [ 76717 ] |
Burt Beckwith
made changes -
| Workflow | jira [ 76717 ] | Grails2 [ 79637 ] |
Stéphane Maldini
made changes -
| Resolution | Fixed [ 1 ] | |
| Status | Closed [ 6 ] | Reopened [ 4 ] |
| Assignee | Stéphane Maldini [ pred ] |
Peter Ledbrook
made changes -
| Last Reviewed | 01/Jan/10 |
Graeme Rocher
made changes -
| Status | Reopened [ 4 ] | Closed [ 6 ] |
| Fix Version/s | 2.0 final [ 11041 ] | |
| Fix Version/s | 1.4-M1 [ 11040 ] | |
| Resolution | Fixed [ 1 ] |
Peter Ledbrook
made changes -
| Workflow | Grails2 [ 79637 ] | jira [ 87391 ] |
Peter Ledbrook
made changes -
| Workflow | jira [ 87391 ] | Grails2 [ 95540 ] |
| Repository | Branch | Revision | Date | Author/Committer | Message |
| Grails Docs | 2.0.x | 619072c... | 2012-03-23 08:51:10 -0700 | Peter Ledbrook / Peter Ledbrook | Add docs for the new importFrom() feature of constraints - |
| Files Changed | |||||
|
MODIFY
src/en/guide/toc.yml
ADD src/en/guide/validation/sharingConstraints.gdoc MODIFY src/en/ref/Constraints.gdoc |
| Repository | Branch | Revision | Date | Author/Committer | Message |
| Grails Docs | 2.1.x | 619072c... | 2012-03-23 08:51:10 -0700 | Peter Ledbrook / Peter Ledbrook | Add docs for the new importFrom() feature of constraints - |
| Files Changed | |||||
|
MODIFY
src/en/guide/toc.yml
ADD src/en/guide/validation/sharingConstraints.gdoc MODIFY src/en/ref/Constraints.gdoc |
| Repository | Branch | Revision | Date | Author/Committer | Message |
| Grails Docs | master | 7d1eeb1... | 2012-03-23 09:37:36 -0700 | Peter Ledbrook / Peter Ledbrook | Add docs for the new importFrom() feature of constraints - |
| Files Changed | |||||
|
MODIFY
src/en/guide/toc.yml
ADD src/en/guide/validation/sharingConstraints.gdoc MODIFY src/en/ref/Constraints.gdoc |
| Repository | Branch | Revision | Date | Author/Committer | Message |
| Grails Docs | 2.2.x | 7d1eeb1... | 2012-03-23 09:37:36 -0700 | Peter Ledbrook / Peter Ledbrook | Add docs for the new importFrom() feature of constraints - |
| Files Changed | |||||
|
MODIFY
src/en/guide/toc.yml
ADD src/en/guide/validation/sharingConstraints.gdoc MODIFY src/en/ref/Constraints.gdoc |
It seems that this feature is desired for quite some time (long time for rapid Grails timeline
). I've found this thread on grails-dev mailing list: http://tinyurl.com/2b9xdy6
Permalinked post by Marc Palmer describes how it can look like. Additionally, it will be nice to have a possibility to filter imported constraints, for example, something like
class UserSignupCommand { ... static constraints = { ... importFrom UserProfile, [firstName:*, lastName:[size, validator]] } }firstName:* or firstName[] will mean that all constraints should be imported, while non empty list will import only specified constrains.