Tasmanski
Tasmanski

Reputation: 67

How to use custom validator in Grails

I'm new in Grails. I would like to use custom validator for matching two passwords during registration in my application. Unfortunetly doesn't work it.

Account.groovy

@Validateable
class Account {

    String login
    String password
    String confirm
    String passwordHashed
    char active
    static transients = ['password', 'confirm']     
    static belongsTo = Employee
    static hasMany = [role:Role]

    static constraints = {
        login unique:true, blank: false
        active nullable: true
        password blank: false, size: 5..15, validator:{ val, obj ->
            if(obj.password != obj.confirm ){
                return ['dontmatch']
            }
        }       
    }

    static mapping = {
        id generator: 'increment'
    }
}

AuthenticationController.groovy

class AuthenticationController {

    def signUp(Account accountInstance){

        if(accountInstance!= null){
            if (accountInstance.hasErrors()) {
                respond accountInstance.errors, view:'signUp'
                return
            }
            else{
                accountInstance.save(flush: true)
            }
        }
    }

}

signUp.gsp

<%@ page import="com.sarna.entity.Account"%>
<%@ page import="com.sarna.entity.Employee"%>
<!DOCTYPE html>
<html>
<head>
<meta name="layout" content="bootstrap-main" />
<title>SARNA</title>
</head>
<body>
    <br />
    <div class="container">
        <div style="margin-top: 50px"
            class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
            <div class="panel panel-info">
                <div class="panel-heading">
                    <div class="panel-title">Sign Up</div>
                    <div
                        style="float: right; font-size: 85%; position: relative; top: -10px">
                        <a id="signinlink"
                            href="${createLink(uri: '/authentication/signIn') }">Sign In</a>
                    </div>
                </div>
                <div class="panel-body">
                    <g:form class="form-horizontal" role="form"
                        url="[resource: accountInstance]"
                        method="POST" controller="Authentication">


                        <g:hasErrors bean="${accountInstance}">
                            <div class="alert alert-danger">
                                <g:renderErrors bean="${accountInstance}" />
                            </div>
                        </g:hasErrors>
                        <div class="form-group">
                            <label for="email" class="col-md-3 control-label">Email</label>
                            <div class="col-md-9">
                                <input type="text" class="form-control" name="email"
                                    placeholder="Email Address">

                            </div>
                        </div>


                        <div class="form-group">
                            <label for="icode" class="col-md-3 control-label">Login</label>
                            <div class="col-md-9">
                                <g:textField type="text" class="form-control" name="login"
                                    required="" value="${accountInstance?.login }"
                                    placeholder="Login" />

                            </div>
                        </div>

                        <div class="form-group">
                            <label for="password" class="col-md-3 control-label">Password</label>
                            <div class="col-md-9">

                                <g:passwordField name="password"
                                    class="form-control ${hasErrors(bean:accountInstance,field:'password','errors')}"
                                    placeholder="Password" />
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="icode" class="col-md-3 control-label">Confirm</label>
                            <div class="col-md-9">
                                <g:passwordField
                                    class="form-control ${hasErrors(bean:accountInstance,field:'password','errors')}"
                                    name="confirm" placeholder="Confirm Password" />
                            </div>
                        </div>

                        <div class="form-group">
                            <!-- Button -->
                            <div class="col-md-offset-3 col-md-9">

                                <g:actionSubmit action="signUp"
                                    class="btn btn-lg btn-success btn-block" value="Sign Up" />
                            </div>
                        </div>
                    </g:form>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

messages.properties

account.password.dontmatch=Podane hasła muszą się zgadzać

I don't understand it. I enter two different passwords and I click signUp button, I don't see errors messages which should be display when validation failed. Could you help me?

Upvotes: 1

Views: 1146

Answers (3)

injecteer
injecteer

Reputation: 20699

The call of validate() is missing:

if (!accountInstance.validate() || accountInstance.hasErrors()) {
  ...
}

Upvotes: 1

Martin Hauner
Martin Hauner

Reputation: 1733

I think instead of the respond in the controller you want

render (view: "signUp", model: [accountInstance: accountInstance])

Your view expects an accountInstance object in the view model to render the errors, so you have to provide it.

You can also just do

return [accountInstance: accountInstance]

If there is a gsp that has the same name as the current action grails will automatically render it.

Upvotes: 0

dmahapatro
dmahapatro

Reputation: 50245

Match the actual value from the validator with the confirm property from the actual object.

password blank: false, size: 5..15, validator:{ val, obj ->
    if( obj.confirm && val != obj.confirm ) { // val is password
        return ['dontmatch']
    }
}

Upvotes: 0

Related Questions