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

有帮助吗?

解决方案 3

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 e.g

<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

其他提示

you should call cuscmd.validate() before checking if the method cuscmd.hasErrors()

CustomerCommand class should have annotation @Validateable:

@grails.validation.Validateable
class CustomerCommand implements Serializable{
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top