簡體   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