Las restricciones en Grails definen reglas de validación, generación de esquemas y CRUD generación de meta daros. Un ejemplo de conjunto de restricciones aplicadas a una clase de domino son:
blank
Propósito
Validar que el String no esta vació
Ejemplo
login(blank:false)
Descripción
Poner en
falso
si un valor del string no puede ser vacióCodigo de Error:
className.propertyName.blank
creditCard
Purpose
To validate that a String value is a valid credit card number
Examples
cardNumber(creditCard:true)Description
Set to
true
if a string should be a credit card number. Internally uses the org.apache.commons.validator.CreditCardValidator
class.Error Code:
className.propertyName.creditCard.invalid
Purpose
To validate that a String value is a valid email address.
Examples
homeEmail(email:true)Description
Set to
true
if a string value is an email address. Internally uses the org.apache.commons.validator.EmailValidator
class.Error Code:
className.propertyName.email.invalid
inList
Purpose
To validate that a value is within a range or collection of constrained values.
Examples
name(inList:["Joe", "Fred", "Bob"] )Description
Constrains a value so that it must be contained within the given list. This constraint influences schema generation.
Error Code:
className.propertyName.not.inList
matches
Purpose
To validate that a String value matches a given regular expression.
Examples
login(matches:"[a-zA-Z]+")Description
Applies a regular expression against a string value.
Error Code:
className.propertyName.matches.invalid
max
Purpose
Ensures a value does not exceed the given maximum value.
Examples
age(max:new Date())price(max:999F)
Description
Sets the maximum value of a class that implements java.lang.Comparable. The same type needs to be used as the property itself. This constraint influences schema generation.
Error Code:
className.propertyName.max.exceeded
maxSize
Purpose
Ensures a value's size does not exceed the given maximum value.
Examples
children(maxSize:25)Description
This constraint influences schema generation.
Error Code:
className.propertyName.maxSize.exceeded
min
Purpose
Ensures a value does not fall below the given minimum value.
Examples
age(min:new Date())price(min:0F)
This constraint influences schema generation.
Referencia:
http://grails.org/doc/1.0.x/
Description
Sets the minimum value of a class that implements java.lang.Comparable. The same type needs to be used as the property itself. This constraint influences schema generation.
Error Code:
className.propertyName.min.notmet
minSize
Purpose
Ensures a value's size does not fall below the given minimum value.
Examples
children(minSize:25)Description
Sets the minimum size of a collection or number property. This constraint influences schema generation.
Error Code:
className.propertyName.minSize.notmet
notEqual
Purpose
Ensures that a property is not equal to the specified value
Examples
login(notEqual:"Bob")Description
Error Code:
className.propertyName.notEqual
nullable
Purpose
Allows a property to be set to
null
. By default Grails does not allow null
values for properties.Examples
age(nullable:true)Description
Set to
true
if the property allows null
values.Error Code:
className.propertyName.nullable
range
Purpose
Uses a Groovy range to ensure that a property's value occurs within a specified range
Examples
age(range:18..65)Description
Set to a Groovy range which can contain numbers in the form of an
IntRange
, dates or any object that implements Comparable
and providesnext
and previous
methods for navigation. This constraint influences schema generation.Error Code:
className.propertyName.range.toosmall
or className.propertyName.range.toobig
scale
Purpose
Set to the desired scale for floating point numbers (i.e., the number of digits to the right of the decimal point).
Examples
salary(scale:2)Description
Set to the desired scale for floating point numbers (i.e., the number of digits to the right of the decimal point). This constraint is applicable for properties of the following types:
java.lang.Float
, java.lang.Double
, and java.math.BigDecimal
(and its subclasses). When validation is invoked, this constraint determines if the number includes more nonzero decimal places than the scale permits. If so, it automatically rounds the number to the maximum number of decimal places allowed by the scale. This constraint does not generate validation error messages.This constraint influences schema generation.
Error Code: N/A
size
Purpose
Uses a Groovy range to restrict the size of a collection or number or the length of a String.
Examples
children(size:5..15)Description
Sets the size of a collection or number property or String length.
Currently this constraint cannot be used in addition to blank or nullable, a custom validator may be added to perform this kind of constraints.This constraint influences schema generation.
Error Code:
className.propertyName.size.toosmall
or className.propertyName.size.toobig
unique
Purpose
Constraints a property as unique at the database level
Examples
login(unique:true)Description
Set to true if the property must be unique (this is a persistent call and will query the database)
Scope of
unique
constraint can be specified by specifying the name of another property of the same class, or list of such names. The semantics in these cases is: pair of constrained property value and scope property value must be unique (or combination of constrained property value and all scope property values must be unique).Example:
group(unique:'department')In the above example
group
name must be unique in one department
but there might be groups with same name in different departments.Another example:
login(unique:['group','department'])In this example
login
must be unique in group
and department
. There might be same logins in different groups or different departments.url
Purpose
To validate that a String value is a valid URL.
Examples
homePage(url:true)Description
Set to
true
if a string value is a URL. Internally uses the org.apache.commons.validator.UrlValidator
class.Error Code:
className.propertyName.url.invalid
Referencia:
http://grails.org/doc/1.0.x/
No hay comentarios:
Publicar un comentario