简体   繁体   中英

grails g:field error not showing

I have this setup, but the validation is not working

index.gsp:

<g:form  name="loginForm" autocomplete="off" controller="company"  action ="save">

<table >

  <tr>

    <td><g:field type="text" name="company" required="true" value="${c?.companyName}" /></td>

  </tr>
</table>

Controller:

def index = { 
def c  =new Company()

//c=params
return [c:c]

}
def save ={}

You need to check for errors using hasErrors method in your form:

<div class="fieldcontain ${hasErrors(bean: yourBean, field: 'yourField', 'error')} required">
    <label for="yourField">
        <g:message code="yourBean.yourField.label" default="yourField" />
        <span class="required-indicator">*</span>
    </label>
    <g:textField name="content" required="" value="${yourBean?.yourField}"/>
</div>

Grails hasErrors documenttaion.

And check in your controller (save action) if the validation has passed using:

    if (!yourBean.save(flush: true)) {
        render(view: "create", model: [yourBean: yourBean])
        return
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM