繁体   English   中英

Grails命令对象未经过验证

[英]Grails command object is not validated

我在我的项目中使用grails web flow进行多形式注册过程。 我创建了实现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)我无法访问视图页面(GSP)中的流量范围对象。 当我从AddCustomer2单击后退按钮时,这是必需的,它应该显示页面,其中包含用户已从流量范围输入的值

<input type="text" class="input" name="Name" value="${customer?.Name}"/>

这是我在AddCustomer1中的输入字段。 请帮助我解决您可能已经面临的这个问题。 提前致谢

你应该调用cuscmd.validate()如果该方法检查之前cuscmd.hasErrors()

CustomerCommand类应该有@Validateable注释:

@grails.validation.Validateable
class CustomerCommand implements Serializable{

我想卢克拉扎罗维奇已经回答了你的第一个问题。 要回答第二个问题:单击后退按钮时必须将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"

} 

UPDATE
尝试在命名对象命名时保持一致,以减少混淆。例如,上面您使用的是flow.cuscmd和flow.customer。 当您在视图中渲染错误时,这将导致您的问题,例如

<g:if test="${customer?.hasErrors()}">
    <g:renderErrors bean="${customer}" as="list" />
</g:if>

在您的情况下,由于您已将对象命名为flow.cuscmd,因此不会呈现错误

暂无
暂无

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

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