【问题标题】:Grails command object is not validatedGrails 命令对象未经过验证
【发布时间】:2013-12-25 11:12:04
【问题描述】:

我在我的项目中使用 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 中的输入字段。请帮助我解决这个您可能已经面临的问题。提前致谢

【问题讨论】:

    标签: grails spring-webflow grails-2.0 grails-plugin grails-controller


    【解决方案1】:

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

    【讨论】:

    • 它正在工作,但没有显示任何验证错误... '
      ' 我将此代码保存在 gsp 中,但仍然没有显示...有什么需要做的...?
    【解决方案2】:

    CustomerCommand 类应该有注解@Validateable:

    @grails.validation.Validateable
    class CustomerCommand implements Serializable{
    

    【讨论】:

    • 你在说什么链接?
    【解决方案3】:

    我认为 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。当您在视图中呈现错误时,这会给您带来问题,例如

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

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

    【讨论】:

    • 感谢您的回复...我使用了相同的代码集,但我对命令对象的命名感到困惑...您能简单解释一下...吗?
    • 谢谢.. 但总的来说,我没有得到正确的教程,包括子流程在内的详细解释.. 我有电子书......但这没有多大帮助.. 你能给我建议吗? ...
    • 我使用了 grails 文档。这似乎足以满足我当时所需要的一切。 grails.org/doc/latest
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多