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:
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.
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: