2014-10-09 39 views
0

我是grails的新手。我想驗證文本字段並在屏幕上顯示錯誤消息。誰可以幫我這個事。爲eg.lastName不輸入。Grails框架中的Textfield驗證

這裏是我的代碼 Person.groovy 類Person {

String firstName 
String lastName 
int age 
static constraints = { 
    lastName(nullable:false, blank:false) 
    } 

GSP

名字

<label for="firstName">FirstName</label> 
<g:textField name="firstName" value="${person.firstName}"></g:textField><br/> 
<label for="age">Age</label> 
<g:textField name="age" value="${person.age}"/><br/> 
<g:actionSubmit value ="save" action="save"/> 

我試過上面的但它不工作。我是否需要進行任何驗證設置?我試圖在智能

+0

什麼是不工作? – injecteer 2014-10-09 13:01:08

回答

0

您的代碼正常工作 您沒有處理任何錯誤,您至少應該使用某些內容來刷新錯誤消息。 你可能想嘗試這樣的事情:

<g:textField name="firsname" value="${person.firstname}"/> 
      <g:hasErrors bean="${person}" field="firstname"> 
        <g:eachError bean="${person}" field="firstname"> 
         <p style="color: red;"><g:message error="${it}"/></p> 
        </g:eachError> 
       </g:hasErrors>