|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
beforeInsert event problemHi All,
I tried to use beforeInsert on a domain class for the first time today, following an example like the one in the docs: class Person { Date dateCreated def beforeInsert = { dateCreated = new Date() } } ... my assignment is different of course but the same principle. All fine except... this is called after validate() so this is not useful at all if you have any "required" constraints on the properties. You could work around it by assigning some non-null value at init time to the property so it survives validation. However this implies that validation is either being performed twice - before the calls to events and again after, or only once before calling events, in which case events can bypass constraints and hence introduce bugs. Anybody got any views on this? I would have thought that the events have to occur before validation, always. Marc --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: beforeInsert event problemOn 24 Apr 2008, at 13:26, Marc Palmer wrote: > Hi All, > > I tried to use beforeInsert on a domain class for the first time > today, following an example like the one in the docs: > > class Person { > Date dateCreated > def beforeInsert = { > dateCreated = new Date() > } > } Actually this gets worse. I am either cursed, stupid, or doing something wrong. I have found: 1) validation is performed before events 2) changes to properties made in the event have no effect Here's my domain class: import java.rmi.server.UID import java.security.* import java.sql.Timestamp class PendingEmailConfirmation { String confirmationToken String userToken Date timestamp = new Date() // Init the unique token def beforeInsert = { println "invoking beforeInsert - conf token is [$confirmationToken]" makeToken() println "after beforeInsert - conf token is [$confirmationToken]" } void makeToken() { def uid = new UID().toString() confirmationToken = uid.encodeAsSHA1Hex() } static constraints = { confirmationToken(size:1..100) userToken(size:1..100) } } This code above in a unit test that instantiates and then saves the object fails to save because "confirmationToken" is null because validation has kicked in, the event never happens and my printlns do not occur. If I change the code so that confirmationToken is init'd with "?": class PendingEmailConfirmation { String confirmationToken = "?" ... Then the output from the test is: invoking beforeInsert - conf token is [?] after beforeInsert - conf token is [?] So tell me... how is this possible? I think I need to raise a jira or two don't I? My gut tells me I'm missing a stupid typo. I hope so. Marc --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: beforeInsert event problem> // Init the unique token
> def beforeInsert = { > println "invoking beforeInsert - conf token is [$confirmationToken]" > makeToken() > println "after beforeInsert - conf token is [$confirmationToken]" > } What happens if you replace the "makeToken()" line with "confirmationToken = 'something'"? > Then the output from the test is: > > invoking beforeInsert - conf token is [?] > after beforeInsert - conf token is [?] But what's in the database? > So tell me... how is this possible? I think I need to raise a jira or two > don't I? > > My gut tells me I'm missing a stupid typo. I hope so. It doesn't look like it, but I wonder what's happening when the closure offloads to the method. If the closure's delegate is overridden and the resolve strategy changed, strange things might happen. But that shouldn't be happening. Cheers, Peter -- Software Engineer G2One, Inc. http://www.g2one.com/ --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: beforeInsert event problemOn 24 Apr 2008, at 14:38, Peter Ledbrook wrote: >> // Init the unique token >> def beforeInsert = { >> println "invoking beforeInsert - conf token is >> [$confirmationToken]" >> makeToken() >> println "after beforeInsert - conf token is >> [$confirmationToken]" >> } > > What happens if you replace the "makeToken()" line with > "confirmationToken = 'something'"? > >> Then the output from the test is: >> >> invoking beforeInsert - conf token is [?] >> after beforeInsert - conf token is [?] > > But what's in the database? Not sure, running in memory and haven't had time to explore. However the property was definitely being left as ? because the data was then used to send an email and the token was ? ... which started the whole sorry tale of how beforeInsert is b0rked in 1.0.2 :) I had to give up completely on using an event, it just isn't working. Code has to go live today... Marc --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
| Free Forum Powered by Nabble | Forum Help |