[Opensource] Quick solution to Form Validation completed
Raul DAVIDOVICH
R.DAVIDOVICH at caconcology.com
Fri Aug 23 02:54:54 PDT 2002
Hello again,
I found what I was missing in order to diaplay the errors in the JSP.
here is the corrected answer:
1) You define the validation rules in the DBObject (many of them are
already set, like null fields.. but this can be overriden). You can also
define masks with the setMask() method
2) The input fields MUST have the same name as the DBObjects fields.
3) You call request.validateDBObject(myDBObject, myErrorCollection). this
in a try/catch block. This method throws two exceptions, a
ControllerException and a ValidationException that must be caught
4) If there are validation errors, the methid
myErrorCollection.getErrorCount will be greater than 0, so there you test
the condition and act accordingly
5) You add the errors to the response with the
saveErrors(myErrorCollection) method
6) In the JSP, you use the <expresso:ErrorTag/> to show the errors (I
couldn't make it work yet.. I'm still digging).. I'll see if I can manage
to display the errors for each field somehow.. I guess Lirian's API could
be useful for it).. for this to be done, the keys errors.header and
errors.footer must be set in MessagesBundle.properties
that's it.
Here is a code example:
...
try{
MyDBObject dbo = new MyDBObject();
ErrorColecction errors = new ErrorCollection();
da.setDBName(params.getDBName());
try{
dbo.validateDBObject(dbo, errors);
}
catch (ControllerException ce){
//do something
}
catch (ValidationException ve){
//do something
}
if(errors.getErrorCount()==0){
//the form was ok.. do something
try{
transition("nextTransition", params, response);
}
catch (NonHandleableException nhe) {
throw new ControllerException("problem after transition");
}
}
else{
//the form was wrong.. do something
try{
response.saveErrors(myErrorCollection);
transition("sameTransition", params, response);
}
catch (NonHandleableException nhe) {
throw new ControllerException("problem after transition");
}
}
}
catch (DBException dbe){
//do something
}
...
In the JSP:
...
<font color="red">
<expresso:ErrorTag/>
</font>
...
hope this will help other people....
Best regards
---------------------------------------------------
Raul Davidovich
Responsable Informatique
Cvitkovic & Associés Consultants
(33) 1 45 15 40 68
(33) 1 45 15 40 41 Fax
-------------------------------------------------------
http://www.caconcology.com
More information about the Opensource
mailing list