Home Grails Command Object redirect with model to have the same url/page
Reply: 0

Grails Command Object redirect with model to have the same url/page

user3377
1#
user3377 Published in July 22, 2018, 8:03 pm

I've created an User domain with username, password and email and I would like to use Command object to check if password repeated and email repeated are the same or not. I've created a UserController:

class UserController {

static scaffold = User

def register() { }

def registration(UserRegistrationCommand urc) {
    if (urc.hasErrors()) {
        render(view: 'register', model: [user: urc])
    } else {
        def user = new User(urc.properties)
        user.profile = new Profile()
        if (user.save()) {
            flash.message = "Congratulations ${user.username}, you've been correctly registered. An email as been sent to ${user.email} in order to activate your account."
            redirect(uri: '/')
        } else {
            return [user: user]
        }
    }
}
}

class UserRegistrationCommand {
    String username
    String password
    String passwordRepeat
    String email
    String emailRepeat
static constraints = {
    importFrom User
    importFrom Profile

    password(size: 6..8, blank: false, validator: {pwd, urc -> return pwd != urc.username})
    passwordRepeat(nullable: false, validator: {pwd, urc -> return pwd == urc.password})

    emailRepeat(nullable: false, email: true, validator: {addr, urc -> 

return addr == urc.email})
    }
}

And only a "register.gsp" view:

    <html>
<head>
    <meta name="layout" content="main">
</head>
<body>
<h1>Register New User</h1>
<g:hasErrors>
    <div class="errors">
        <g:renderErrors bean="${user}" as="list" />
    </div>
</g:hasErrors>
<g:if test="${flash.message}">
    <div class="flash">${flash.message}</div>
</g:if>
<g:form controller="user" action="registration" method="POST" >
<fieldset class="form">
    <div class="fieldcontain required">
        <label for="username">Username</label>
        <g:textField name="username" required="" value="${user?.username}" />
    </div>
    <div class="fieldcontain required">
        <label for="password">Password</label>
    <g:passwordField name="password" required="" />
    </div>
    <div class="fieldcontain required">
        <label for="passwordRepeat">Repeat Password</label>
        <g:passwordField name="passwordRepeat" required="" />
    </div>
    <div class="fieldcontain required">
        <label for="email">Email</label>
        <g:textField name="email" required="" type="email" value="${user?.email}" />
    </div>
    <div class="fieldcontain required">
        <label for="emailRepeat">Repeat Email</label>
        <g:textField name="emailRepeat" required="" />
    </div>
</fieldset>
    <fieldset class="buttons">
        <g:submitButton name="register" value="Register" />
    </fieldset>
</g:form>
</body>
</html>

When I compile form with errors which will call "urc.errors", it redirects me to /user/registration and will appear errors there instead of /user/register as I would like to. Is there any way to use only "user/register" page to compile form and submit data or edit form if data is incorrect?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306602 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO