Commons validator sur un exemple tout simple?
Salut,
Je souhaite tester l'api Commons Validator sur un exemple tout simple.
J'ai une classe Name qui contient deux attributs, firstName et lastName :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class Name {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
} |
(vous voyez, on ne peut pas plus simple)
J'ai récupéré (et adapté) un bout de code qui effectue la validation, à partir de la description dans validation.xml :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import java.io.InputStream;
import java.util.Map;
import org.apache.commons.validator.Validator;
import org.apache.commons.validator.ValidatorResources;
import org.apache.commons.validator.ValidatorResults;
public class Test {
public static void main(String[] args) throws Exception {
InputStream in = Test.class.getResourceAsStream("/validation.xml");
// Create an instance of ValidatorResources to
// initialize from an xml file.
ValidatorResources resources = new ValidatorResources(in);
// Create bean to run test on.
Name name = new Name();
// Construct validator based on the loaded resources
// and the form key
Validator validator = new Validator(resources, "nameForm");
// add the name bean to the validator as a resource
// for the validations to be performed on.
validator.setParameter(Validator.BEAN_PARAM, name);
// Get results of the validation.
ValidatorResults results = validator.validate();
if (results.getValidatorResult("firstName") == null) {
// no error
System.out.println("ok");
} else {
// TODO
}
}
} |
Le problème maintenant: le fichier XML.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1.dtd">
<form-validation>
<formset>
<form name="nameForm">
<field property="firstName" depends="required">
<arg0 key="nameForm.firstname.displayname"/>
</field>
<field property="lastName" depends="required">
<arg0 key="nameForm.lastname.displayname"/>
</field>
</form>
</formset>
</form-validation> |
Je veux juste (vu que c'est pour un test) tester si firstName et lastName ont bien une valeur (donc depends="required").
arg0, je ne sais pas à quoi il fait référence, et dans mon test je ne veux pas utiliser l'internationalisation.
Voici ce qu'il se passe quand j'exécute Test :
Code:
1 2 3 4 5 6
| Exception in thread "main" org.apache.commons.validator.ValidatorException: No ValidatorAction named required found for field firstName
at org.apache.commons.validator.Field.handleMissingAction(Field.java:897)
at org.apache.commons.validator.Field.validate(Field.java:873)
at org.apache.commons.validator.Form.validate(Form.java:288)
at org.apache.commons.validator.Validator.validate(Validator.java:351)
at Test.main(Test.java:28) |
Pouvez-vous m'aider à corriger mon .xml pour qu'il fasse ce que je veux svp?
J'ai trouvé pas mal d'exemples sur internet, mais c'est toujours combiné à plein de choses Struts... Je voudrais vraiment un exemple minimal pour voir comment fonctionne cet outil.
Merci d'avance.
®om