Discussion:
How to validate boolean field in the constraints.
Jianfeng
2009-06-18 09:08:41 UTC
Permalink
Hi all,

I have a boolean field in the domain class.

class A {
boolean fieldA

static constraints = {
fieldA(nullable: true)
}
}

The purpose that I set nullable constraint to this field is to make sure the
user has to select one of the boolean values.

During the application start up, I got the warning in the log as below:

WARN [util.GrailsUtil] [WARNING] Property [fieldA] of domain class A has
type [boolean] and doesn't support constraint [nullable]. This constraint
will not be checked during validation.


Does anybody know how to do boolean value constraint?

Thanks

Jet
--
View this message in context: http://www.nabble.com/How-to-validate-boolean-field-in-the-constraints.-tp24089103p24089103.html
Sent from the grails - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Burt Beckwith
2009-06-18 12:44:23 UTC
Permalink
Change it to

Boolean fieldA

If you save null to a column Hibernate will throw a NPE on load for primitives like boolean, int, long, etc. since it has no default mapping for what null means. You might want it to default to false but other might want it to default to true. Using the wrapper classes allows real null mapping and lets you handle it yourself.

Burt
Post by Jianfeng
Hi all,
I have a boolean field in the domain class.
class A {
boolean fieldA
static constraints = {
fieldA(nullable: true)
}
}
The purpose that I set nullable constraint to this field is to make sure the
user has to select one of the boolean values.
WARN [util.GrailsUtil] [WARNING] Property [fieldA] of domain class A has
type [boolean] and doesn't support constraint [nullable]. This constraint
will not be checked during validation.
Does anybody know how to do boolean value constraint?
Thanks
Jet
Jianfeng
2009-06-24 10:19:54 UTC
Permalink
Hi,

Thanks guys. It works.

Jet
Post by Burt Beckwith
Change it to
Boolean fieldA
If you save null to a column Hibernate will throw a NPE on load for
primitives like boolean, int, long, etc. since it has no default mapping
for what null means. You might want it to default to false but other might
want it to default to true. Using the wrapper classes allows real null
mapping and lets you handle it yourself.
Burt
Post by Jianfeng
Hi all,
I have a boolean field in the domain class.
class A {
boolean fieldA
static constraints = {
fieldA(nullable: true)
}
}
The purpose that I set nullable constraint to this field is to make sure the
user has to select one of the boolean values.
WARN [util.GrailsUtil] [WARNING] Property [fieldA] of domain class A has
type [boolean] and doesn't support constraint [nullable]. This constraint
will not be checked during validation.
Does anybody know how to do boolean value constraint?
Thanks
Jet
--
View this message in context: http://www.nabble.com/How-to-validate-boolean-field-in-the-constraints.-tp24089103p24182159.html
Sent from the grails - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Herbert Baeuerle
2009-06-18 13:11:42 UTC
Permalink
You can always use validator:
The problem is boolean is not an object (primitive).

fieldA( validator: {
val, obj ->
if ( !val) return return ['invalid']
})

Cheers
Herbert
Post by Jianfeng
Hi all,
I have a boolean field in the domain class.
class A {
boolean fieldA
static constraints = {
fieldA(nullable: true)
}
}
The purpose that I set nullable constraint to this field is to make sure the
user has to select one of the boolean values.
WARN [util.GrailsUtil] [WARNING] Property [fieldA] of domain class A has
type [boolean] and doesn't support constraint [nullable]. This constraint
will not be checked during validation.
Does anybody know how to do boolean value constraint?
Thanks
Jet
--
http://www.nabble.com/How-to-validate-boolean-field-in-the-constraints.-tp24089103p24089103.html
Sent from the grails - user mailing list archive at Nabble.com.
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
Scott Burch
2009-06-18 14:37:30 UTC
Permalink
I'm not sure why you would want to. The problem only occurs when you
add a boolean property with an already existing database.

Simply go into the database and set the column to the default value.

update foo set bar = 0 (for false)

That will solve the problem.
Post by Jianfeng
Hi all,
I have a boolean field in the domain class.
class A {
boolean fieldA
static constraints = {
fieldA(nullable: true)
}
}
The purpose that I set nullable constraint to this field is to
make sure the
user has to select one of the boolean values.
WARN [util.GrailsUtil] [WARNING] Property [fieldA] of domain class A has
type [boolean] and doesn't support constraint [nullable]. This constraint
will not be checked during validation.
Does anybody know how to do boolean value constraint?
Thanks
Jet
--
http://www.nabble.com/How-to-validate-boolean-field-in-the-constraints.-tp24089103p24089103.html
Sent from the grails - user mailing list archive at
Nabble.com.
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Scott Burch
2009-06-18 14:54:28 UTC
Permalink
I just noticed what you wrote. booleans only have two values. This is
a non-issue.
Post by Scott Burch
I'm not sure why you would want to. The problem only occurs when you
add a boolean property with an already existing database.
Simply go into the database and set the column to the default value.
update foo set bar = 0 (for false)
That will solve the problem.
Post by Jianfeng
Hi all,
I have a boolean field in the domain class.
class A {
boolean fieldA
static constraints = {
fieldA(nullable: true)
}
}
The purpose that I set nullable constraint to this field is to
make sure the
user has to select one of the boolean values.
WARN [util.GrailsUtil] [WARNING] Property [fieldA] of domain
class A has
type [boolean] and doesn't support constraint [nullable]. This
constraint
will not be checked during validation.
Does anybody know how to do boolean value constraint?
Thanks
Jet
--
http://www.nabble.com/How-to-validate-boolean-field-in-the-constraints.-tp24089103p24089103.html
Sent from the grails - user mailing list archive at
Nabble.com.
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Lucas F. A. Teixeira
2009-06-18 16:58:43 UTC
Permalink
a boolean that can be null?

it seems like a troolean :)

[]s,


Lucas Frare Teixeira .ยท.
- ***@gmail.com
- blog.lucastex.com
- twitter.com/lucastex
Post by Jianfeng
Hi all,
I have a boolean field in the domain class.
class A {
boolean fieldA
static constraints = {
fieldA(nullable: true)
}
}
The purpose that I set nullable constraint to this field is to make sure the
user has to select one of the boolean values.
WARN [util.GrailsUtil] [WARNING] Property [fieldA] of domain class A has
type [boolean] and doesn't support constraint [nullable]. This constraint
will not be checked during validation.
Does anybody know how to do boolean value constraint?
Thanks
Jet
--
http://www.nabble.com/How-to-validate-boolean-field-in-the-constraints.-tp24089103p24089103.html
Sent from the grails - user mailing list archive at Nabble.com.
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
Loading...