简体   繁体   English

使用域对象约束失败的grails 3 gsp

[英]grails 3 gsp using domain object constraints failing

In grails 2 we were able to reference the domain object constraints in a gsp as to keep the html 5 configration dry. 在grails 2中,我们能够在gsp中引用域对象约束,以使html 5配置保持干燥。 On grails 3 (tried both 3.1.10 and 3.2.0.RC1) I get an error for code I tested in grails 2 successfully. 在grails 3上(尝试了3.1.10和3.2.0.RC1),我在grails 2中成功测试的代码出现错误。 I am trying to reference the constraint matches in the attribute phone and use that for the HTML 5 pattern. 我正在尝试在属性phone中引用约束匹配并将其用于HTML 5模式。 The scaffolding use to generate this code but for Grails 3 the scaffolding generates use the fields plugin so I cannot see that code. 脚手架用于生成此代码,但是对于Grails 3,脚手架用于生成使用fields插件,因此我看不到该代码。 Any ideas? 有任何想法吗?

Here is the domain object code: 这是域对象代码:

class Disruption {

static constraints = {
    phone(matches:/^[0-9]{10}$/, nullable:true)
    email(email:true, nullable:false)
}

String name
String phone
String email

Here is the gsp code: 这是gsp代码:

    <div class="form-group ${hasErrors(bean: disruption, field: 'phone', 'error')}">
    <label for="phone" class="control-label col-sm-3">
        Phone
    </label>
    <div class="col-sm-2">
        <g:textField name="phone" style="width: 7em" class="form-control" title="Phone 10 digits" pattern="${disruption.constraints.phone.matches}" maxlength="10" placeholder="##########" value="${disruption.phone}"/>
    </div>
</div>

Here is the exception: 这是例外:

URI /disruption/create Class java.lang.NullPointerException Message Request processing failed; URI / disruption / create类java.lang.NullPointerException消息请求处理失败; nested exception is org.grails.gsp.GroovyPagesException: Error processing GroovyPageView: [views/disruption/create.gsp:92] Error executing tag : Error evaluating expression [disruption.constraints.phone.matches] on line [58]: Cannot get property 'phone' on null object Caused by Cannot get property 'phone' on null object 嵌套的异常是org.grails.gsp.GroovyPagesException:处理GroovyPageView时出错:[views / disruption / create.gsp:92]执行标签时出错:在第[58]行上对表达式[disruption.constraints.phone.matches]的计算出错:无法获取空对象上的属性“ phone”由以下原因引起:无法在空对象上获得属性“ phone”

Domain Objects need to use the constrainedProperties and Command Object need to use the constraintsMap see examples below. 域对象需要使用constrainedProperties,命令对象需要使用constraintsMap,请参见下面的示例。

            <g:textField name="phone" style="width: 7em" class="form-control" title="Phone 10 digits" pattern="${disruption.constrainedProperties.phone.matches}" maxlength="10" placeholder="##########" value="${disruption?.phone}"/>

OR for Command Objects 或命令对象

            <g:textField name="phone" style="width: 7em" class="form-control" title="Phone 10 digits" pattern="${searchCommand.constraintsMap.phone.matches}" maxlength="10" placeholder="##########" value="${searchCommand?.phone}"/>

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

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