官术网_书友最值得收藏!

Control through constraints

While this in itself is pretty impressive, with a few minor changes to your domain class, you can control the order in which the fields are displayed. You can also perform some basic validation to make sure that the users aren't entering rubbish into the application. Add the following highlighted code in the User.groovy class file:

package app
class User {
String username
String title
String firstName
String lastName
String password
Date dateCreated
Date lastModified
static constraints = { username(blank: false, size: 4..20, unique:true) title(blank:false, inList:["", "Dr", "Miss", "Mr", "Mrs"]) firstName(blank: false, size:1..20) lastName(blank: false, size:1..30) password(blank: false, size:6..20, password:true) dateCreated(nullable: true) lastModified(nullable: true) } 
}

This is your first introduction to the DSL that Grails provides for web development. Defining constraints on the domain class using the above syntax provides a simple and readable way of specifying rules for your domain objects.

The definition of the constraints property may look unfamiliar at this point. The constraints property references a closure (or code block). Each line in the code block declares the constraints for an individual property. You will notice that the methods being called in the code block have not been declared anywhere. This is possible because a Groovy Builder is being used to intercept the method calls. The builder uses the arguments as the constraints for the property that matches the method name. You will learn more about Groovy Builders in Chapter 4.

The first use of constraints, with regard to scaffolding, is the order. Grails scaffolding uses the order in which the constraints are declared in the domain class to determine the display order of fields on the scaffolded screens. This ordering applies to the create, edit, and list screens.

The second use of constraints within scaffolding is to determine which input type is used when rendering a form. The username, the firstName and the lastName fields will all be rendered as simple text inputs, while the password field is now rendered as a password input. By using the inList constraint for the title field, scaffolding will render an HTML select input field containing the options specified in the constraint.

The main use of constraints is to enforce the validity of the data in our application. We can set rules regarding whether a property is required, how many characters are allowed, and whether the property must be unique. If we take the constraints we have added to the user class as an example, we have specified that:

  • The username cannot be blank. It must be between 4 and 20 characters and must be unique
  • The title cannot be blank and must be one of these options: 'Dr', 'Miss', 'Mr', or 'Mrs'
  • First name cannot be blank and must be in the range of 1 to 20 characters
  • Last name cannot be blank and must be in the range of 1 to 30 characters
  • Password cannot be blank and must be in the range of 6 to 20 characters

If you go back to the Create User page, you should see that the order of the fields displayed is the same as the order defined by the constraints property as shown in the following screenshot:

Control through constraints

Now, try to create a user without specifying all the required information, and you will find that the Grails scaffolding automatically performs the validation checks on the data entered for a user.

Control through constraints

As you have seen in your User class example, it is possible to use a number of constraints for a single property. At the time of writing this book, the following default constraints were available:

主站蜘蛛池模板: 虹口区| 阆中市| 曲水县| 青铜峡市| 宜阳县| 禄劝| 重庆市| 永平县| 金平| 涿鹿县| 灵武市| 滦南县| 陇川县| 静宁县| 玉溪市| 普洱| 闸北区| 夏邑县| 扎赉特旗| 原平市| 石嘴山市| 防城港市| 玛曲县| 澄江县| 聊城市| 武鸣县| 凤山市| 肥东县| 丁青县| 当涂县| 中牟县| 蚌埠市| 凤台县| 晋江市| 甘孜县| 沧源| 万盛区| 拉萨市| 姜堰市| 桂阳县| 泽州县|