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

Joe*_*Joe 2 grails grails3

在grails 2中,我们能够在gsp中引用域对象约束,以保持html 5 configration的干燥.在grails 3(尝试3.1.10和3.2.0.RC1)上,我在grails 2中成功测试的代码出错了.我试图在属性手机中引用约束匹配,并将其用于HTML 5模式.脚手架用于生成此代码但是对于Grails 3,脚手架生成使用fields插件,因此我看不到该代码.有任何想法吗?

这是域对象代码:

class Disruption {

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

String name
String phone
String email
Run Code Online (Sandbox Code Playgroud)

这是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>
Run Code Online (Sandbox Code Playgroud)

这是一个例外:

URI/disruption/create类java.lang.NullPointerException消息请求处理失败; 嵌套异常是org.grails.gsp.GroovyPagesException:处理GroovyPageView时出错:[views/disruption/create.gsp:92]执行标记时出错:在[58]上评估表达式[disruption.constraints.phone.matches]时出错:无法获取null对象上的属性'phone'引起无法在null对象上获取属性'phone'

Joe*_*Joe 5

Domain Objects需要使用constrainedProperties和Command Object需要使用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}"/>
Run Code Online (Sandbox Code Playgroud)

或命令对象

            <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}"/>
Run Code Online (Sandbox Code Playgroud)