2017-12-31 147 views
0

ユーザ名、パスワード、電子メールでユーザドメインを作成しました。パスワードの繰り返しと電子メールの繰り返しが同じかどうかをCommandオブジェクトで確認したいと思います。私はUserControllerでを作成しました:Grailsコマンドオブジェクトモデルと同じURL /ページを持つリダイレクト

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}) 
    } 
} 

そして唯一の「register.gsp」ビュー:

<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> 

私は「urc.errors」と呼ぶエラーでフォームをコンパイルすると、それは私をリダイレクトします/ user/registrationの代わりに/ user/registerの代わりにエラーが表示されます。データが正しくない場合、フォームをコンパイルしてデータを送信したり、フォームを編集するために「ユーザー/登録」ページのみを使用する方法はありますか?

答えて

0

registerregistrationのアクションが必要ですか?あなたはただ1つのアクションを持つことができ、ユーザーがフォームを送信したときにのみ応答します。

コントローラー:

... 
def register(UserRegistrationCommand urc) { 
     if (params.register) { 
      if (urc?.hasErrors()) { 
       [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] 
       } 
      } 
     } 
    } 
... 

GSP:

... 
<g:form controller="user" action="register" method="POST" > 
... 
+0

私は「params.register」を使用して考えていなかったので、私は、私はいつもそれため、コマンドオブジェクトを使用するために2つのアクションを使用していたと思いました"null"エラーが返されました。どうもありがとうございます!それは私がそれが欲しかったのです! :) –

関連する問題