PDA

View Full Version : Commons Validator - Multiple validations on the same form



NicolasPeeters
Feb 6th, 2006, 09:02 AM
Hi,

I am implementing the Commons Validator (in my SWF app). I am using my domain object as formBackingObject, I therefore want to be able to use mulitple validation rules (in validation.xml, for example) in each of my 3 different screens.

The problem is that the form name is always the saeme, since I am referring to the same (domain) object for all my screens. When I debug I can see that the FormSet always points to the same fields to be validated, it therefore skips validation of the fields in my second and third (which are other fields in my same FormBackingObject).

- What would the recommended approach be?
- Shall I create 3 factories?
- What about the use of the ConfigurableBeanValidator?

Thanks a lot.

Nicolas

NicolasPeeters
Feb 6th, 2006, 09:23 AM
I had a look further on the forum and it seems that the NamedBeanValidator might be what I am looking for... It has apparently been renamed to ConfigurableBeanValidator. It doesn't seem to work, though... Here's my config. Any idea what could be wrong?



<bean id="validator1" class="org.springmodules.commons.validator.ConfigurableBe anValidator">
<property name="formName" value="form1" />
<property name="validatorFactory" ref="validatorFactory" />
</bean>

<bean id="validator2" class="org.springmodules.commons.validator.ConfigurableBe anValidator">
<property name="formName" value="form2" />
<property name="validatorFactory" ref="validatorFactory" />
</bean>


<bean id="validatorFactory"
class="org.springmodules.commons.validator.DefaultValidat orFactory">
<property name="validationConfigLocations">
<list>
<value>classpath:/validator-rules.xml</value>
<value>classpath:/validation.xml</value>
</list>
</property>
</bean>


validation.xml


<form-validation>
<formset>
<form name="form1">
<field property="clientName" depends="required">
<arg0 key="projectCredential.clientName" />
</field>
<field property="projectName" depends="required">
<arg0 key="projectCredential.projectName" />
</field>
</form>
<form name="form2">
<field property="fees" depends="required">
<arg0 key="projectCredential.fees" />
</field>
<field property="projectLocation" depends="required">
<arg0 key="projectCredential.projectLocation" />
</field>
</form>
</formset>
</form-validation>

bigears
Feb 27th, 2006, 11:51 AM
Hi,

Just noticed your post regarding a Multiple validations on the same form. We are currently facing the same issue. Any ideas/suggestions as to how to deal with this?

We are using the commons validators (in server-side) within the Spring Webflow framework with one large bean which is used across many pages.

Many thanks.

NicolasPeeters
Feb 27th, 2006, 12:00 PM
Ok, this should help you. I saw this solution on one of the posts. Here's the complete configuration.

validationContext.xml


<bean id="validatorFactory"
class="org.springmodules.commons.validator.DefaultValidat orFactory">
<property name="validationConfigLocations">
<list>
<value>classpath:/validator-rules.xml</value>
<value>classpath:/validation.xml</value>
</list>
</property>
</bean>


<bean id="validatorTemplate"
class="org.springmodules.commons.validator.ConfigurableBe anValidator"
abstract="true">
<property name="validatorFactory" ref="validatorFactory" />
</bean>

<bean id="validator1" parent="validatorTemplate">
<property name="formName" value="form1" />
</bean>

<bean id="validator2" parent="validatorTemplate">
<property name="formName" value="form2" />
</bean>


Create a class that extends SWF's FormAction:


public class FormActionTemplate extends FormAction {

private static final String VALIDATOR = "validator";
private Map validators;

public Event bindAndValidate(RequestContext ctx) throws Exception {
AttributeSource propMap = ctx.getProperties();
if (propMap.containsAttribute(VALIDATOR)) {
String valName = (String) propMap.getAttribute(VALIDATOR);
if (!validators.containsKey(valName))
throw new IllegalArgumentException(
"Invalid validator name: "+ valName);

Validator val = (Validator) validators.get(valName);
setValidator(val);
}
return super.bindAndValidate(ctx);
}


Now, make sure that your action class extends the class I just pasted above. FYI, in my example ProjectAction extends FormActionTemplate.

In flow configuration file, at the right view, call the appropriate validator.



<!-- First form in the flow -->
<view-state id="step1" view="projectInformationForm">
<entry>
<action bean="projectAction" method="setupForm" />
</entry>
<transition on="submit" to="step2">
<action bean="projectAction" method="bindAndValidate">
<property name="validator" value="validator1" />
</action>
</transition>
<transition on="error" to="tryAgain" />
</view-state>


Hope it helps! Good luck.

Nicolas

NicolasPeeters
Feb 27th, 2006, 12:03 PM
I realized I forgot to paste my validation configuration for the different forms.
validation.xml. Note that I am using the same object for both forms...



<form-validation>
<formset>
<form name="form1">
<field property="clientName" depends="required">
<arg0 key="projectCredential.clientName" />
</field>
<field property="projectName" depends="required">
<arg0 key="projectCredential.projectName" />
</field>
</form>

<form name="form2">
<field
property="teamSolution.processImprovements"
depends="required">
<arg0 key="projectCredential.teamSolution.processImprovements" />
</field>
</form>

bigears
Feb 27th, 2006, 12:23 PM
Many thanks Nicolas for your prompt reply.

Does this assume that the Action is configured to be (Non) Singleton as I cannot see how this will work for another user (thread) if one thread is changing the action's validator on the bindAndValidate method.

I would be grateful for more clarification on this.

Regards.

NicolasPeeters
Feb 27th, 2006, 12:40 PM
Hmm, to be honest I didn't consider that issue before. :-S
Can you explain me a bit more about how this could be a problem? What would be the difference with the original FormAction from SWF? We actually only add another validator to the form action class...

Hopefully one of the senior SWF people can give us more insight here...?

Any thoughts?

Nicolas

bigears
Feb 27th, 2006, 01:29 PM
Can you explain me a bit more about how this could be a problem ...

Well as the FormAction which is a Singleton by default which has a single validator object at any point in time then changing this by one thread will have an affects on all other users (threads). This is how I understand it unless off-course I am mistaken.

It would be good to have the view of some senior people please...

anicad
May 16th, 2006, 02:37 PM
I do not understand how the attribute validators ever gets populated in your example. Could you please explain further?
This line:
private Map validators;

Thanks!

Sam Brannen
May 16th, 2006, 03:17 PM
Hi Everybody,

I've just posted some comments on a similar thread regarding "declarative wizard-style validation". Have a look here:

http://forum.springframework.org/showpost.php?p=61642&postcount=7

I am open to feedback.

Thanks,

Sam

rprabhakar
Jul 20th, 2006, 10:18 AM
I was unable to understand how to use Validation1 and Validation2 for two different controllers.

rprabhakar
Jul 20th, 2006, 10:28 AM
I am getting following error:

Error creating bean with name 'validator2' defined in ServletContext resource [/WEB-INF/claimsos-servlet.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyAccessExceptions Exception: PropertyAccessExceptionsException (1 errors); nested propertyAccessExceptions are: [org.springframework.beans.TypeMismatchException: Failed to convert property value of type [org.springmodules.validation.commons.DefaultValida torFactory] to required type [org.springmodules.commons.validator.ValidatorFacto ry] for property 'validatorFactory'

Sam Brannen
Jul 20th, 2006, 10:37 AM
Hi Rohit,



[org.springframework.beans.TypeMismatchException: Failed to convert property value of type [org.springmodules.validation.commons.DefaultValida torFactory] to required type [org.springmodules.commons.validator.ValidatorFacto ry] for property 'validatorFactory'

Verify which release of Spring Modules you're using. Note the bold sections above: as you can see, one of them refers to "commons.validator" and the other refers to "validation.commons". The validation packages were renamed in release 0.4 of Spring Modules. So, it would appear that your config is based on a release that is different from the JAR you are using.

Refer to the change log (https://springmodules.dev.java.net/source/browse/*checkout*/springmodules/changelog.txt) for details.

Regards,

Sam

rprabhakar
Jul 21st, 2006, 01:44 AM
I am using Spring Modules 0.4

Sam Brannen
Jul 21st, 2006, 05:53 AM
I am using Spring Modules 0.4

Were you able to resolve your version conflict?

If not, we could probably assist you better if you post your Spring applicationContext configuration for your validatorFactory, beanValidator, controller, etc.

- Sam

rprabhakar
Jul 21st, 2006, 06:04 AM
Thanks

I found the problem as I was trying to get the ConfigurableBeanValidator from a wrong package.