Duplicate Grails Error Messages
I am new to grails and I have a problem:
I have this GSP snippet:
<g:form url="[controller:'main',action:'login']">
<label for="name">Usuario:</label><br/>
<input type="text" name="name" /><br/>
<label for="pass">Password:</label><br/>
<input type="password" name="password"/><br/>
<input type="submit" value="Login"/><br/>
<g:renderErrors bean="${cmd}"/>
</g:form>
Controller (MainController.groovy) uses Command Object, here's the code for both:
def login = { LoginCommand cmd ->
if(cmd.validate()){
redirect(action:'ok')
}else{
render(view:'index',model:[cmd:cmd])
}
}
class LoginCommand {
String name
String password
static constraints = {
name(blank:false,size:5..10)
password(blank:false,size:5..10)
}
}
The problem is when I enter a wrong username or password (empty or out of range) , it shows me 4 errors, two for the password and two for the username . They are the same, but duplicated.
I found that creating a method "bool validateCommand () {name && password}" and replacing it for command.validate () does not throw duplicates, but I want to use Grails constraint functions to keep things DRY.
Any idea why this is happening? Many thanks!
a source to share
When you add command objects to controller actions, Grails will execute validate()
automatically, so there is no need to manually call it. Try
if(!cmd.hasErrors())
instead
if(cmd.validate())
It seems that every call validate()
adds new (repeated) errors to the command object. IMHO this shouldn't happen and is probably a bug in Grails. You should consider reporting this issue.
a source to share