grails - Grails命令对象未通过验证

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

我在我的项目中使用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/

相关文章:

grails - 有没有办法做空安全的GORM动态查找器?

spring-webflow - 支持通过 Spring Cloud Gateway 代理 SOAP/XML 端点

spring - 如何在异常时设置文字字符串 flowScope 值?

grails - Cereal 清洁不起作用

java - grails找不到以下匹配的构造函数:somiti.JointMember(java.lang.String)

Grails YAML 映射列表

grails - grails 3.2.6无法初始化测试数据 “is not a domain class or GORM has not been initialized correctly”

java - 在Spring Webflow中设置多个if else条件决策状态id

facebook - 如何从 Grails 中的 Spring Security 的 Facebook 身份验证中获取 Facebook 用户名、电子邮件、电话号码等

ajax - Cereal ;动态更新的remoteFunction在HTML页面中导致null