commons validator without struts

R

Ricardo Trindade

Hi,

I' having troubles using commons-validator (latest release, also tried
today's snapshot) without struts.

Specifically I tried the code in CVS that can be reached through the FAQ
item :
http://wiki.apache.org/jakarta-commons/ValidatorStandalone

I couldn't understand why this examples uses Validators from the test
package, instead of using, for example, GenericValidator. I tried to change
the example to use GenericValidator, but validating my bean always fails.

My XML (header removed):

<form-validation>
<global>
<validator name="inteiros"
classname="org.apache.commons.validator.GenericValidator" method="isInt"
methodParams="java.lang.String" msg="errors.int"/>
</global>
<formset>
<form name="ValidateBean">
<field property="firstName" depends="inteiros"/>
</form>
</formset>
</form-validation>


thanks
Ricardo Trindade
 
M

Mark Mersereau

Hi Ricardo,

Ricardo Trindade said:
Hi,

I' having troubles using commons-validator (latest release, also tried
today's snapshot) without struts.

Specifically I tried the code in CVS that can be reached through the FAQ
item :
http://wiki.apache.org/jakarta-commons/ValidatorStandalone

I couldn't understand why this examples uses Validators from the test
package, instead of using, for example, GenericValidator. I tried to change
the example to use GenericValidator, but validating my bean always fails.

[snip]

I got this example to work by

1. Moving a couple methods from
org.apache.struts.validator.FieldChecks to my own
org.apache.commons.validator.example.TestValidator class

2. Adjusting the class and method names in the validator-example.xml
to match my TestValidator class:

-----------------------------------------------------------------------------
package org.apache.commons.validator.example;

import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericTypeValidator;
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.util.ValidatorUtils;

/**
* @author mmersereau
*
* To change this generated comment edit the template variable
"typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class TestValidator {

public static boolean validateRequired(Object bean, Field field)
{

String value = null;
if (isString(bean)) {
value = (String) bean;
} else {
value = ValidatorUtils.getValueAsString(bean,
field.getProperty());
}

if (GenericValidator.isBlankOrNull(value)) {
return false;
} else {
return true;
}

}

public static Integer validateInteger(Object bean, Field field)
{
Integer result = null;
String value = null;
if (isString(bean)) {
value = (String) bean;
} else {
value = ValidatorUtils.getValueAsString(bean,
field.getProperty());
}

if (!GenericValidator.isBlankOrNull(value)) {
result = GenericTypeValidator.formatInt(value);
}

return result;
}

protected static boolean isString(Object o) {
return (o == null) ? true : String.class.isInstance(o);
}
}
-----------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top