aspose file tools*
The moose likes Groovy and the fly likes Changing Domain object's constraints on the fly. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Groovy
Bookmark "Changing Domain object Watch "Changing Domain object New topic
Author

Changing Domain object's constraints on the fly.

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

So, I have a basic constraints for my domain class, that will allow a few fields to be nullable. But later in my app, for another use case, I will need them to be not nullable. Now I can't just set it to be not nullable, because the domain object is also used as my UserDetails object in Spring Security, and those fields need allow nulls at the time of registration.

Anyway, I created two sets of constraints and made a "submitValidation" closure that gets called to swap the constraints, then do a validate() call, but that doesn't seem to do the magic trick.




Does anyone have any other recommendations?

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

I *think* that your scenario is the reason for Command Objects.


GenRocket - A Test Data Generation Platform
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Gregg Bolinger wrote:I *think* that your scenario is the reason for Command Objects.


Ouch, I like always using my domain objects as Form backing beans. With the Command object, that would mean i need to create an adapter class to get the values from one "domain" object to another. I'd actually rather do a whole validation method in my controller rather than another domain object like a command object.

Mark
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Actually it isn't what command objects are for. ;)

Grails Command Objects provide a simple mechanism to validate form fields that do not map directly to domain objects.


1) Actually there isn't any form fields on the page that will do the submit.
2) If there were fields, they all map directly to my Domain object.

Basically the Band object has its own form, which the original constraints are for.

There is a link on my left nav that is a "submit to app store" link (can be clicked at any time on any page), that will take the user to a purchase page.
when they click the submit to app store link, the method behind it will get the Band object from the Spring Security Context, since I am using the domain object as my User object for Spring Security, then it will call validate on the Band object, in which the new constraints need to be used to validate.

Does this make sense? There is no form with fields or a backing object needed.

Thanks

Mark
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Yep, I get what you saying. What you really need is some sort of conditional validation. I've looked high and low for something like that, but haven't been able to track anything down. Let me know if you find something.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

BTW, Rails has Conditional Validation. Maybe hit the grails mailing list?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Gregg Bolinger wrote:BTW, Rails has Conditional Validation. Maybe hit the grails mailing list?


Thanks for looking. I was also trying to avoid the grails mailing list because then I get 100 emails from it just saying that it doesn't know if my post even got sent to the mailing list, that it was still "waiting" and this goes on for a good month.

Mark
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

This is one reason I don't like model-based validation, which was one of my biggest beefs.

IMO it's solving a lower-level problem: in what states is this object completely invalid? But that stops working when the object is used in multiple contexts. Something like Costanza's Context-Oriented Programming makes sense--seems like a context could just throw something on top of an object that defines how it may be used depending on the current context.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Changing Domain object's constraints on the fly.
 
Similar Threads
[Grails] Passing user object into hiddenField
Grails custom error messages
MissingMethodException Error
How to autharize against Oracle DB with an LDAP Authentication
Passing objects across controllers in Grails