简体   繁体   中英

Grails command object is not validated

I am using grails web flow for multiform registration process in my project. I created the command object which implements Serializable.

 class CustomerCommand implements Serializable{

String Name
Integer Age
Date DateOfBirth
String FatherOrHusbandName
String IdProof
String IdProofNumber

static constraints = {
}
}

My flow section

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")        

    }   
   }

Now I am facing two problems.

1) When I click next button, the hasErrors() function is not properly validating the form input values. It simply redirects to AddCustomer2 page. It accepts blank values also.

2) I am not able to access the flow scope object in view page(GSP). This is required when I click back button from AddCustomer2, it should show the page with values which are already entered by the user from flow scope

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

This is my input field in AddCustomer1. Kindly help me anyone to fix this issue which you might have faced already. Thanks in advance

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

CustomerCommand class should have annotation @Validateable :

@grails.validation.Validateable
class CustomerCommand implements Serializable{

I think lukelazarovic already answered your first question. To answer your second question: Have to add the commandobj to the flow when the back button is clicked like this:

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
Try to be consistent in you naming of the command objects too to reduce confusion For example above you are using flow.cuscmd and flow.customer. This will cause problems for you when you are rendering errors in your view eg

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

In your case errors won't be rendered because you have named the object flow.cuscmd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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