Reputation: 2459
How can I check in Grails controller-action that all the required parameters has been provided to this action method. In other words, can I simply specify using a map or list object for all the parameters which are required for my action.
I can get the params and do an explicit check in each action but that is lot of code just for simple validation. Is there a simple way so that a default error message is returned with the missing parameter name.
EDIT
There are two things I think I forgot to mention - 1. These controller-action are for implementing webservices and they are not linked to a GSP. 2. The required params need vary greatly from action to action. Keeping that in mind using command object does not seem correct method. every method is going to have different required param and we can not create command object for each web service.
Spring has something similar where we can specify for each action, which are the params and whether they are required or optional.
Upvotes: 2
Views: 2982
Reputation: 50275
Command
Objects are handy in those scenarios. You can define a command object with the params
bindings and add constraints on each/all of them. You can also use custom validator
to validate the request parameters (params
).
for example:
//Command Object
class LoginCommand{
String userName
String password
static constraints = {
//Constraints and validator defined here
}
}
Accessing command object in Controller:
def myAction(LoginCommand lc){
if(lc.hasErrors()){//redirect to error page or do something else}
}
You can find more details here.
Putting request parameter validations in command objects has below pros:
params
during execution of action
, they act as business objects/beans where you can handle domain (application business domain) related logic as well.UPDATE
Here is a sample code how Command Object can be used for validations for all the actions
in a controller
.
Upvotes: 2
Reputation: 23906
If you are using the data from params to populate an instance of a Domain class you can just set the constraints accordingly, populate an instance of the domain class, and use the validate method.
If the data you want to validate is not really related to a Domain class, you can still use about the same validation mechanism, by creating a Command Object and specifying it in your controller action.
This is the Command Object example from the docs:
@grails.validation.Validateable
class LoginCommand {
String username
String password
static constraints = {
username(blank: false, minSize: 6)
password(blank: false, minSize: 6)
}
}
And then in the controller:
class LoginController {
def login(LoginCommand cmd) {
if (cmd.hasErrors()) {
redirect(action: 'loginForm')
return
}
// work with the command object data
}
}
Relevant quote from the docs:
Before the controller action is executed Grails will automatically create an instance of the command object class and populate its properties by binding the request parameters. If the command object class is marked with Validateable then the command object will be validated.
Upvotes: 2