我在我的项目中使用grails Web流进行多种形式的注册过程。我创建了实现Serializable的命令对象。
class CustomerCommand implements Serializable{
String Name
Integer Age
Date DateOfBirth
String FatherOrHusbandName
String IdProof
String IdProofNumber
static constraints = {
}
}
我的流程部分
def customerRegisterFlow = {
enter {
action {
Customer flow.customer = new Customer()
[customer: flow.customer]
}
on("success").to("AddCustomer1")
}
AddCustomer1 {
on("next") { CustomerCommand cuscmd ->
if(cuscmd.hasErrors()) {
flash.message = "Validation error"
flow.cuscmd = cuscmd
return error()
}
bindData(flow.customer, cuscmd)
[customer: flow.customer]
}.to("AddCustomer2")
}
}
现在我面临两个问题。
1)当我单击下一步按钮时,hasErrors()函数未正确验证表单输入值。它只是重定向到AddCustomer2页面。它也接受空白值。
2)我无法在 View 页面(GSP)中访问流作用域对象。当我单击AddCustomer2中的“后退”按钮时,这是必需的,它应显示带有用户已从流范围中输入的值的页面
<input type="text" class="input" name="Name" value="${customer?.Name}"/>
这是我在AddCustomer1中的输入字段。请帮助我解决您可能已经遇到的此问题。提前致谢
最佳答案
我认为lukelazarovic已经回答了您的第一个问题。要回答第二个问题:单击后退按钮时,必须将commandobj添加到流中,如下所示:
AddCustomer2 {
on("next") { CustomerCommand cuscmd ->
if(cuscmd.hasErrors()) {
flash.message = "Validation error"
flow.cuscmd = cuscmd
return error()
}
bindData(flow.customer, cuscmd)
[customer: flow.customer]
}.to("finish")
on("back"){CustomerCommand customer->
flow.customer= customer
}.to "AddCustomer1"
}
更新
在命名命令对象时也要尽量保持一致,以减少混乱
例如,在上面,您正在使用flow.cuscmd和flow.customer。当您在 View 中呈现错误时,这将为您带来问题,例如
<g:if test="${customer?.hasErrors()}">
<g:renderErrors bean="${customer}" as="list" />
</g:if>
在您的情况下,由于已将对象命名为flow.cuscmd,因此不会呈现错误。
关于grails - Grails命令对象未通过验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20772130/