简体   繁体   English

grails g:字段错误未显示

[英]grails g:field error not showing

I have this setup, but the validation is not working 我有此设置,但是验证不起作用

index.gsp: 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: 您需要在表单中使用hasErrors方法检查错误:

<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. Grails hasErrors文档。

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
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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