|
Hi all,
Is anyone aware of a nice way to validate transient fields? I am currently using a transient field to upload a multipart file which is then saved to disk. The file itself is not stored in my database via GORM, however a string giving the file's path is. In my controller I call a service to store the file to disk and retrieve the path to the file which is then set on my domain class. It would be really nice to be able to define a custom validator in my domain class to check the type, size etc. of the multipart file. If this isn't possible are there any recommended alternatives? Your thoughts are appreciated. Cheers, Dan Shilcock --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
Assuming you want to ensure that the transient can only have certain properties on save, then use the static contraints to achieve it.
static transients = ['myTransient'] static constraints = {
if (!myTransient || myTransient >> OUT_OF_BOUNDS) { }errors.reject('myTransient is invalid') }On Thu, Feb 21, 2008 at 9:26 AM, <[hidden email]> wrote: Hi all, |
|
In reply to this post by mail-94
Take a look at here:
http://grails.org/GORM+-+Creating+a+domain+class Interesting, there is no information about "transients" in the user guide. Kind Regards On Thu, Feb 21, 2008 at 6:26 AM, <[hidden email]> wrote: Hi all, -- Marcos Silva Pereira http://marcospereira.wordpress.com "You only live once.", James Brown |
|
In reply to this post by j pimmel
Hi,
It tried to do what you suggested, but it doesn't compile:
My code:
class User {
boolean agree
static transients = ["agree"] static constraints = {
if(!agree){ errors.reject('foo') } } }
This generates the following error:
groovy.lang.MissingPropertyException: No such property: agree for class: User
...so I assume the syntax is faulty.
I have tried with the following constraint:
static constraints = {
agree(validator:{return (it) }) }
This does what I want (accepts only if agree is True) as long as the field 'agree' is not transient. The problem as far as I understand is that when I declare the field transient, a call to validate() or save() does not check against the constraint at all.
I would be very thankful if someone could tell how this is supposed to be done.
Regards,
Markus Samuelsson
2008/2/21, j pimmel <[hidden email]>:
Assuming you want to ensure that the transient can only have certain properties on save, then use the static contraints to achieve it. |
|
Remember that you are in a static scope (constraints is static). What you must to do is define a custom validator:
static constraints = { agree(validator: { value, domain -> /* do what you need here */ }) } Kind Regards On Thu, Feb 21, 2008 at 9:26 AM, Markus Samuelsson <[hidden email]> wrote:
-- Marcos Silva Pereira http://marcospereira.wordpress.com "You only live once.", James Brown |
|
In reply to this post by Markus Samuelsson
have you tried
def agree instead of boolean the transients we are checking in our working code are loosely declared as defs On Thu, Feb 21, 2008 at 12:26 PM, Markus Samuelsson <[hidden email]> wrote:
|
|
Hi All,
The only way around this that I have found so far is to create a helper method in the domain class which performs the validation: void validateImage() { if(image.getSize() > MAX_ALLOWED_SIZE) { this.errors.rejectValue("image", "some.code", "Image too big!") } } } And then in my controller both the standand and helper validators are called: def save = { def myDomainClass = new MyDomainClass(params) //Ensure validation takes place, including custom image validation rentalProperty.validate() rentalProperty.validateImage() ........ } Quoting j pimmel <[hidden email]>: > have you tried > > def agree > > instead of boolean > > the transients we are checking in our working code are loosely declared as > defs > > On Thu, Feb 21, 2008 at 12:26 PM, Markus Samuelsson < > [hidden email]> wrote: > >> Hi, >> >> It tried to do what you suggested, but it doesn't compile: >> >> My code: >> >> class User { >> boolean agree >> static transients = ["agree"] >> static constraints = { >> if(!agree){ >> errors.reject('foo') >> } >> } >> } >> This generates the following error: >> groovy.lang.MissingPropertyException: No such property: agree for class: >> User >> >> ...so I assume the syntax is faulty. >> >> >> I have tried with the following constraint: >> static constraints = { >> agree(validator:{return (it) }) >> } >> This does what I want (accepts only if agree is True) as long as the field >> 'agree' is not transient. The problem as far as I understand is that >> when I declare the field transient, a call to validate() or save() does >> not check against the constraint at all. >> I would be very thankful if someone could tell how this is supposed to be >> done. >> >> Regards, >> Markus Samuelsson >> >> >> >> >> 2008/2/21, j pimmel <[hidden email]>: >> >> > Assuming you want to ensure that the transient can only have certain >> > properties on save, then use the static contraints to achieve it. >> > >> > static transients = ['myTransient'] >> > static constraints = { >> > if (!myTransient || myTransient >> OUT_OF_BOUNDS) { >> > errors.reject('myTransient is invalid') >> > >> > } >> > >> > } >> > >> > On Thu, Feb 21, 2008 at 9:26 AM, <[hidden email]> wrote: >> > >> > > Hi all, >> > > >> > > Is anyone aware of a nice way to validate transient fields? I am >> > > currently using a transient field to upload a multipart file which is >> > > then saved to disk. >> > > >> > > The file itself is not stored in my database via GORM, however a >> > > string giving the file's path is. >> > > >> > > In my controller I call a service to store the file to disk and >> > > retrieve the path to the file which is then set on my domain class. >> > > >> > > It would be really nice to be able to define a custom validator in my >> > > domain class to check the type, size etc. of the multipart file. If >> > > this isn't possible are there any recommended alternatives? >> > > >> > > Your thoughts are appreciated. >> > > >> > > Cheers, >> > > >> > > Dan Shilcock >> > > >> > > >> > > --------------------------------------------------------------------- >> > > To unsubscribe from this list, please visit: >> > > >> > > http://xircles.codehaus.org/manage_email >> > > >> > > >> > > >> > >> > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
In reply to this post by j pimmel
My validator ( agree(validator:{return (it) }) ) is Ok since it works when the field is not set to transient.
I tried using def instead of booelan, but still the same problem.
J Pimmel, could you send an example where you are using constraints validation of transient field (where it's working :) )
/Markus
2008/2/21, j pimmel <[hidden email]>:
have you tried |
|
In reply to this post by Marcos Silva Pereira
Hi Marcos,
This issue here is that constraints, either standard or custom do not work when applying them to transient fields, they are simply ignored. Thanks, Dan. Quoting Marcos Silva Pereira <[hidden email]>: > Remember that you are in a static scope (constraints is static). What you > must to do is define a custom validator: > > static constraints = { > agree(validator: { value, domain -> /* do what you need here */ }) > } > > Kind Regards > > On Thu, Feb 21, 2008 at 9:26 AM, Markus Samuelsson < > [hidden email]> wrote: > >> Hi, >> >> It tried to do what you suggested, but it doesn't compile: >> >> My code: >> >> class User { >> boolean agree >> static transients = ["agree"] >> static constraints = { >> if(!agree){ >> errors.reject('foo') >> } >> } >> } >> This generates the following error: >> groovy.lang.MissingPropertyException: No such property: agree for class: >> User >> >> ...so I assume the syntax is faulty. >> >> >> I have tried with the following constraint: >> static constraints = { >> agree(validator:{return (it) }) >> } >> This does what I want (accepts only if agree is True) as long as the field >> 'agree' is not transient. The problem as far as I understand is that >> when I declare the field transient, a call to validate() or save() does >> not check against the constraint at all. >> I would be very thankful if someone could tell how this is supposed to be >> done. >> >> Regards, >> Markus Samuelsson >> >> >> >> >> 2008/2/21, j pimmel <[hidden email]>: >> >> > Assuming you want to ensure that the transient can only have certain >> > properties on save, then use the static contraints to achieve it. >> > >> > static transients = ['myTransient'] >> > static constraints = { >> > if (!myTransient || myTransient >> OUT_OF_BOUNDS) { >> > errors.reject('myTransient is invalid') >> > >> > } >> > >> > } >> > >> > On Thu, Feb 21, 2008 at 9:26 AM, <[hidden email]> wrote: >> > >> > > Hi all, >> > > >> > > Is anyone aware of a nice way to validate transient fields? I am >> > > currently using a transient field to upload a multipart file which is >> > > then saved to disk. >> > > >> > > The file itself is not stored in my database via GORM, however a >> > > string giving the file's path is. >> > > >> > > In my controller I call a service to store the file to disk and >> > > retrieve the path to the file which is then set on my domain class. >> > > >> > > It would be really nice to be able to define a custom validator in my >> > > domain class to check the type, size etc. of the multipart file. If >> > > this isn't possible are there any recommended alternatives? >> > > >> > > Your thoughts are appreciated. >> > > >> > > Cheers, >> > > >> > > Dan Shilcock >> > > >> > > >> > > --------------------------------------------------------------------- >> > > To unsubscribe from this list, please visit: >> > > >> > > http://xircles.codehaus.org/manage_email >> > > >> > > >> > > >> > >> > > > -- > Marcos Silva Pereira > http://marcospereira.wordpress.com > "You only live once.", James Brown > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
In reply to this post by j pimmel
Apologies, small naming correction to my example: def save = { def myDomainClass = new MyDomainClass(params) //Ensure validation takes place, including custom image validation myDomainClass.validate() myDomainClass.validateImage() ........ } Quoting j pimmel <[hidden email]>: > have you tried > > def agree > > instead of boolean > > the transients we are checking in our working code are loosely declared as > defs > > On Thu, Feb 21, 2008 at 12:26 PM, Markus Samuelsson < > [hidden email]> wrote: > >> Hi, >> >> It tried to do what you suggested, but it doesn't compile: >> >> My code: >> >> class User { >> boolean agree >> static transients = ["agree"] >> static constraints = { >> if(!agree){ >> errors.reject('foo') >> } >> } >> } >> This generates the following error: >> groovy.lang.MissingPropertyException: No such property: agree for class: >> User >> >> ...so I assume the syntax is faulty. >> >> >> I have tried with the following constraint: >> static constraints = { >> agree(validator:{return (it) }) >> } >> This does what I want (accepts only if agree is True) as long as the field >> 'agree' is not transient. The problem as far as I understand is that >> when I declare the field transient, a call to validate() or save() does >> not check against the constraint at all. >> I would be very thankful if someone could tell how this is supposed to be >> done. >> >> Regards, >> Markus Samuelsson >> >> >> >> >> 2008/2/21, j pimmel <[hidden email]>: >> >> > Assuming you want to ensure that the transient can only have certain >> > properties on save, then use the static contraints to achieve it. >> > >> > static transients = ['myTransient'] >> > static constraints = { >> > if (!myTransient || myTransient >> OUT_OF_BOUNDS) { >> > errors.reject('myTransient is invalid') >> > >> > } >> > >> > } >> > >> > On Thu, Feb 21, 2008 at 9:26 AM, <[hidden email]> wrote: >> > >> > > Hi all, >> > > >> > > Is anyone aware of a nice way to validate transient fields? I am >> > > currently using a transient field to upload a multipart file which is >> > > then saved to disk. >> > > >> > > The file itself is not stored in my database via GORM, however a >> > > string giving the file's path is. >> > > >> > > In my controller I call a service to store the file to disk and >> > > retrieve the path to the file which is then set on my domain class. >> > > >> > > It would be really nice to be able to define a custom validator in my >> > > domain class to check the type, size etc. of the multipart file. If >> > > this isn't possible are there any recommended alternatives? >> > > >> > > Your thoughts are appreciated. >> > > >> > > Cheers, >> > > >> > > Dan Shilcock >> > > >> > > >> > > --------------------------------------------------------------------- >> > > To unsubscribe from this list, please visit: >> > > >> > > http://xircles.codehaus.org/manage_email >> > > >> > > >> > > >> > >> > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
In reply to this post by mail-94
2008/2/21, [hidden email] <[hidden email]>:
Hi Marcos, Is this behaviour by design, or is it a bug?
/Markus |
|
I have created a new post to discuss it:
http://www.nabble.com/Validation-does-not-work-for-transient-attributes-td15611613.html To me, this is a bug (so I post a test case in the new post). Kind Regards On Thu, Feb 21, 2008 at 10:46 AM, Markus Samuelsson <[hidden email]> wrote:
-- Marcos Silva Pereira http://marcospereira.wordpress.com "You only live once.", James Brown |
|
In reply to this post by Markus Samuelsson
Hi Markus, It looks like there is already a JIRA report for this issue: http://jira.codehaus.org/browse/GRAILS-1263 I'm not sure if this is by design or not. Thanks, Dan. Quoting Markus Samuelsson <[hidden email]>: > 2008/2/21, [hidden email] <[hidden email]>: >> >> Hi Marcos, >> >> This issue here is that constraints, either standard or custom do not >> work when applying them to transient fields, they are simply ignored. >> >> Thanks, >> >> Dan. > > > Is this behaviour by design, or is it a bug? > /Markus > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
To everyone who wants to validate transient field:
Vote for bug report http://jira.codehaus.org/browse/GRAILS-1263 :)
/Markus
2008/2/21, [hidden email] <[hidden email]>:
|
|
In reply to this post by j pimmel
On Thursday 21 February 2008 03:45, j pimmel wrote:
> Assuming you want to ensure that the transient can only have certain > properties on save, then use the static contraints to achieve it. > > static transients = ['myTransient'] This is new to me. I've been using a "transient" keyword on domain class fields I want excluded from persistence. Nor can I find this documented (a thorough grep of all the files in the Guide shows zero occurrences of "transients"). Can you point me to any documentation? > static constraints = { > if (!myTransient || myTransient >> OUT_OF_BOUNDS) { > errors.reject('myTransient is invalid') > } > } Randall Schulz --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
http://grails.org/GORM+-+Creating+a+domain+class
2008/2/21, Randall R Schulz <[hidden email]>:
On Thursday 21 February 2008 03:45, j pimmel wrote: |
|
In reply to this post by Randall R Schulz
Hi Randall,
You can find the information on transients here: http://grails.org/GORM+-+Creating+a+domain+class Thanks, Dan. Quoting Randall R Schulz <[hidden email]>: > On Thursday 21 February 2008 03:45, j pimmel wrote: >> Assuming you want to ensure that the transient can only have certain >> properties on save, then use the static contraints to achieve it. >> >> static transients = ['myTransient'] > > This is new to me. I've been using a "transient" keyword on domain class > fields I want excluded from persistence. Nor can I find this documented > (a thorough grep of all the files in the Guide shows zero occurrences > of "transients"). Can you point me to any documentation? > > >> static constraints = { >> if (!myTransient || myTransient >> OUT_OF_BOUNDS) { >> errors.reject('myTransient is invalid') >> } >> } > > > Randall Schulz > > --------------------------------------------------------------------- > To unsubscribe from this list, please visit: > > http://xircles.codehaus.org/manage_email > > > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
In reply to this post by Markus Samuelsson
On Thursday 21 February 2008 08:03, Markus Samuelsson wrote:
> 2008/2/21, Randall R Schulz <[hidden email]>: > > On Thursday 21 February 2008 03:45, j pimmel wrote: > > > ... > > > > > > static transients = ['myTransient'] > > > > This is new to me. I've been using a "transient" keyword on domain > > class fields I want excluded from persistence. Nor can I find this > > documented (a thorough grep of all the files in the Guide shows > > zero occurrences of "transients"). Can you point me to any > > documentation? > > > > ... > > http://grails.org/GORM+-+Creating+a+domain+class I was asking about the Guide. I really do not want to have to read a Wiki, with content scattered across dozens of tiny pages, to get comprehensive information. Randall Schulz --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
I couldn't find any information in the guide either.
Dan. Quoting Randall R Schulz <[hidden email]>: > On Thursday 21 February 2008 08:03, Markus Samuelsson wrote: >> 2008/2/21, Randall R Schulz <[hidden email]>: >> > On Thursday 21 February 2008 03:45, j pimmel wrote: >> > > ... >> > > >> > > static transients = ['myTransient'] >> > >> > This is new to me. I've been using a "transient" keyword on domain >> > class fields I want excluded from persistence. Nor can I find this >> > documented (a thorough grep of all the files in the Guide shows >> > zero occurrences of "transients"). Can you point me to any >> > documentation? >> > >> > ... >> >> http://grails.org/GORM+-+Creating+a+domain+class > > I was asking about the Guide. > > I really do not want to have to read a Wiki, with content scattered > across dozens of tiny pages, to get comprehensive information. > > > Randall Schulz > > --------------------------------------------------------------------- > To unsubscribe from this list, please visit: > > http://xircles.codehaus.org/manage_email > > > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
| Powered by Nabble | Edit this page |
