2012-10-19 61 views
0

我試圖呈現一些錯誤的觀點,但錯誤只出現在第二次提交。來自hasErrors的錯誤只出現在第二次提交

//Controller Method 
def submitUserInfo = { UserDetailsCommand command -> 



    if(!session.upgradeActive) { 
     return upgradeProcessEnded(); 
    } 

    User user = springSecurityService.currentUser; 

    if(!user.userDetails) { 

     user.userDetails = new UserDetails(params); 

     user.userDetails.user = user; 
    } 
    else { 

     user.userDetails.properties = params; 
    } 

    if (!command.hasErrors()) { 

     if (!params.firstName) 
     { 
      user.userDetails.errors.rejectValue('firstName',message(code: 'base.subscription.errors.generalOutput', args: [message(code: 'base.user.firstName.label')])) 
     } 
     if (!params.lastName) 
     { 
      user.userDetails.errors.rejectValue('lastName',message(code: 'base.subscription.errors.generalOutput', args: [message(code: 'base.user.lastName.label')])) 
     } 

     // Internal Server Error Code 
     response.status = 400 
     return render (view: 'userInfo', model: [userInstance: user]) 
    } 
    ... 
} 

//Class 
@Validateable 
class UserDetailsCommand { 

    // Properties 
    String firstName 
    String lastName 
    String phoneNumber 

    //Company 
    String companyName 
    String companyPhoneNumber 
    String address 
    String postalCode 
    String country 
    String city 
    String state 

    static constraints = { 
     firstName nullable: false 
     lastName nullable: false 
     address nullable: false 
     postalCode nullable: false 
     country nullable: false 
     city nullable: false 
     state nullable: false 
     companyName nullable: false 
    } 
} 

//View 
<g:if test='${flash.message}'> 
     <div class="control-group primary-background"> 
      <div class='alert alert-error'> 
       <i class="icon-exclamation-sign"></i> 
       ${flash.message} 
      </div> 
     </div> 
     <%-- Clear messages to prevent showing on refresh --%> 
     ${flash.clear()} 
    </g:if> 
<g:hasErrors bean="${userInstance.userDetails}"> 
<div class="control-group primary-background"> 
    <p class="alert alert-error"> 
     <g:each var="error" in="${userInstance.userDetails.errors.allErrors}"> 
      <i class="icon-exclamation-sign"></i> 
      <g:message error="${error}" /> 
      <br /> 
     </g:each> 
    </p> 
</div> 
</g:hasErrors> 

<div class="flex_column one_half first"> 

<g:hiddenField name="id" value="${ userInstance?.id }"/> 

<h3><g:message code="base.user.details.title" /></h3> 

<strong><g:message code="base.user.email.label" />: </strong> 
${fieldValue(bean: userInstance, field: "username")} 

<label for="firstName"><g:message code="base.user.firstName.label" /></label> 
<g:textField name="firstName" value="${fieldValue(bean: userInstance.userDetails, field: "firstName")}"/> 

<label for="lastName"><g:message code="base.user.lastName.label" /></label> 
<g:textField name="lastName" value="${fieldValue(bean: userInstance.userDetails, field: "lastName")}"/> 
... 

那麼,在第一次提交錯誤後顯示沒有錯誤出現。 當我更改驗證規則,空白字段時,案例再次重複。 如果在第一次提交時這些字段爲空白,則在第二次提交後出現錯誤時不會顯示錯誤。 有什麼建議嗎?

+0

內部服務器錯誤是500不是400 ... –

回答

1

你只顯示代碼塊if (!command.hasErrors()) {

如果您提交空白firstNamelastName,它應該有錯誤。

如果沒有,你應該可能使用blank: false代替nullable: false爲您驗證了firstNamelastName,而不是試圖推出自己的驗證?

相關問題