grails 3 gsp 使用域对象约束失败
grails 3 gsp using domain object constraints failing
在 grails 2 中,我们能够在 gsp 中引用域对象约束,以保持 html 5 配置干燥。在 grails 3(尝试了 3.1.10 和 3.2.0.RC1)上,我在 grails 2 中成功测试的代码出现错误。我正在尝试引用属性 phone 中的约束匹配并将其用于 HTML 5 模式。脚手架用于生成此代码,但对于 Grails 3,脚手架生成使用字段插件,因此我看不到该代码。有什么想法吗?
这是域对象代码:
class Disruption {
static constraints = {
phone(matches:/^[0-9]{10}$/, nullable:true)
email(email:true, nullable:false)
}
String name
String phone
String email
这是 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>
例外情况:
URI
/disruption/create
Class
java.lang.NullPointerException
信息
请求处理失败;嵌套异常是 org.grails.gsp.GroovyPagesException:处理 GroovyPageView 时出错:[views/disruption/create.gsp:92] 执行标记时出错:在第 [58] 行评估表达式 [disruption.constraints.phone.matches] 时出错:无法获取 属性 'phone' 在空对象上
引起的
无法在空对象
上获取 属性 'phone'
域对象需要使用 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}"/>
或命令对象
<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}"/>
在 grails 2 中,我们能够在 gsp 中引用域对象约束,以保持 html 5 配置干燥。在 grails 3(尝试了 3.1.10 和 3.2.0.RC1)上,我在 grails 2 中成功测试的代码出现错误。我正在尝试引用属性 phone 中的约束匹配并将其用于 HTML 5 模式。脚手架用于生成此代码,但对于 Grails 3,脚手架生成使用字段插件,因此我看不到该代码。有什么想法吗?
这是域对象代码:
class Disruption {
static constraints = {
phone(matches:/^[0-9]{10}$/, nullable:true)
email(email:true, nullable:false)
}
String name
String phone
String email
这是 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>
例外情况:
URI /disruption/create Class java.lang.NullPointerException 信息 请求处理失败;嵌套异常是 org.grails.gsp.GroovyPagesException:处理 GroovyPageView 时出错:[views/disruption/create.gsp:92] 执行标记时出错:在第 [58] 行评估表达式 [disruption.constraints.phone.matches] 时出错:无法获取 属性 'phone' 在空对象上 引起的 无法在空对象
上获取 属性 'phone'域对象需要使用 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}"/>
或命令对象
<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}"/>