|
Usage of Command Objects for validation or data bean. Back to Validation
Grails Command Objects provide a simple mechanism to validate form fields that do not map directly to domain objects. Using the validation constraints concept that applies to domain classes, including the validate() method and "errors" dynamic property, Grails applies the concept of command objects to controller actions.
Controller actions may optionally specify any number of command object parameters. The parameter types must be supplied so that Grails knows what objects to create, populate and validate. Before the controller action is executed Grails will create an instance of the command object class, populate the properties of the command object with request parameters having corresponding names and the command object will be validated.
class AccountController {
def save { ValidateInputFieldCommand valCmd -> if(valCmd.hasErrors()) { render(view:'create',model:[valCmd:valCmd, account:account]) } else { do the work .... } } }
In case of an error Grails uses the default error message, but what Grails displays is probably not what you were after, so you will want to change this. The way you do this is by modifying the "grails-app/i18n/messages.properties" file and adding a message for the particular error code.
validateInputFieldCommand.inputField.invalidCash=Missing Receipt Number validateInputFieldCommand.inputField.invalidCreditCard=The Credit Card Number [{2}] is invalid validateInputFieldCommand.inputField.invalidCheckNo=Missing Check Number validateInputFieldCommand.inputField.invalidAccount=Account No [{2}] is invalid
The command object class may be defined under src/groovy/ or under grails-app/controllers/. The command object class may also be defined in the same source file as the controller that uses it.
You can use them as validator for page input fields which have no reference to a domain object or as data bean layer between the controller and the page (GSP).
Sample:
class ValidateInputFieldCommand implements Serializable {
private static String PAYMENT_TYPE_CREDIT_CARD = A private static String PAYMENT_TYPE_CASH = C private static String PAYMENT_TYPE_DOMESTIC_CHECK = D private static String PAYMENT_TYPE_FOREIGN_CHECK = F
String paymentType String inputField //entry data depending on payment type
static constraints = {
inputFiled(maxSize:20, validator: { val, obj -> if ( obj.paymentType == PAYMENT_TYPE_CASH ) { if ( !val || val.length() == 0 ) return ['invalidCash'] } else if ( obj.paymentType == PAYMENT_TYPE_DOMESTIC_CHECK || obj.paymentType == PAYMENT_TYPE_FOREIGN_CHECK ) { if ( !val || val.length() == 0 ) return ['invalidCheckNo'] } else if ( obj.paymentType == PAYMENT_TYPE_CREDIT_CARD ) { // creditCard:true, only if col is credit Card# if ( !obj.validateCreditCardNumber(val, obj) ) return 'invalidCreditCard'] } else if ( obj.paymenType == PAYMENT_TYPE_CHARGE_ON_ACCOUNT ) { if ( !obj.validateAccountNumber(val) ) return ['invalidAccount'] }
})
}
private boolean validateCreditCardNumber(String creditCardNumber, def obj) { if ( obj.checkCreditCard() ) // call method of own instance return true else return false }
private boolean validateAccountNumber(String accountNo) { def account = Account.get(accountNo) if ( account ) return true else return false }
}
Back to Validation
|