PDA

View Full Version : bind and validate


anicad
Apr 5th, 2006, 11:20 AM
is it possible to bind and validate mutliple instances of a domain object at one time? or do you have to create an instance, then bindAndValidate, then create the next instance, then bindAndValidate?

For example,

my FormAction bean is defined below:
On entry of my view state, i want to create two instances of the SearchCriteria object and bind and validate both of them. is that possible?

Thanks in advance.

<bean id="formAction" class="org.springframework.webflow.action.FormAction">
<property name="formObjectName" value="searchCriteria"/>
<property name="formObjectClass" value="org.springframework.webflow.samples.phonebook.doma in.SearchCriteria"/>
<property name="formObjectScope" value="FLOW"/>
<property name="validator">
<bean class="org.springframework.webflow.samples.phonebook.doma in.SearchCriteriaValidator"/>
</property>
</bean>

epitoman
Apr 5th, 2006, 02:54 PM
Maybe this is far away from good practises and little to heavyweighted but you could override the createFormObject to return a Proxy of your form object which actually contains two instances of the same class and then intercept the calls to the setter methods and distribute them among your two instances. We're using this to allow binding a single parameter to multiple instances in collections.

Another approach and more straight forward would be to create a form with a collection of searchCriterias and the bind it in your jsp page like this (assuming you have two objects in the collection)

<spring:bind path="command.searchCriterias[0].someField">
...
</spring:bind>
<spring:bind path="command.searchCriterias[1].someField">
...
</spring:bind>

anicad
Apr 10th, 2006, 06:00 PM
how do i instantiate two objects on a form upon entry of a view state?

epitoman
Apr 11th, 2006, 10:59 AM
If it's important they should be created when entering a view state you could add an entry-actions like this

<view-state id="view" view="/myview.jsp">
<entry-actions>
<action bean="mybean.action" method="setupCriterias"/>
</entry-action>
...

and then implement that method in you action

public class MyAction {
...
public Event setupCriterias(RequestContext requestContext) {
MyForm form = (MyForm)getFormObject(requestContext);
form.addCriteria(new SearchCriteria());
form.addCriteria(new SearchCriteria());
return success();
}
}


but if it isn't important and you would like your flow to "remember" entered values you just let your form initialize it for you and you done.