Re: AW: Using DynaValidatorForms in StrutsCX
David Stabel <[email protected]>
| Newsgroups | gmane.comp.java.strutscx.user |
|---|---|
| Organization | Oce Printing Systems GmbH |
| Message-ID | <[email protected]> |
Hi all,
Thank you Bernhard for your answer to my question.
However I did set the 'debugxml=true' parameter in the action of the
form and that had the same effect when typing it in the location field
of the browser.
Anyway I want to tell how I solved the (my) problem.
I wrote my own 'CustomValidatorForm' extending the 'ValidatorForm' of
the Struts framework (org.apache.struts.validator.ValidatorForm). Here I
extended the validate() method to return an error container
(org.apache.struts.action.ActionErrors) with human readable form errors
(e.g. 'email.errors.required'). See below for the validate() method's
java code. In my Action class I manually call this validate() method on
the form and add the returned error container (if any) to the request
context using the 'XML_ERROR' flag which is defined in the
StrutsCXConstants class. Now the occurred errors will be present in the
XML under the 'errors' element. The presence of an occured error can now
be detected and an appropiate action can be taken. See below for and
example to check for the 'email required' error.
As you will notice, I control the validate() method manually by calling
it in the Action class. This way I'm able to add the errors to the
request context for use by the StrutsCX servlet. For this reason I did
not use the DynaValidatorForm because the validate() method in this form
is contolled by the Validator framework as far as I know. That means
that there is no change to add the errors to the request context and
therefore the errors will not show up in the XML. The latter was my
problem :)
--------------------------- Checking ''email required error sample
-----------------------------------------------
<xsl:if test="count(//errors/email.errors.required)!=0">
<!-- display error message -->
</xsl:if>
--------------------------- END Checking ''email required error sample
-----------------------------------------------
--------------------------- Validate() method's java code sample
-----------------------------------------------
/**
* Validate the properties that have been set from this HTTP request,
* and return an <code>ActionErrors</code> object that encapsulates any
* validation errors that have been found. If no errors are found, return
* <code>null</code> or an <code>ActionErrors</code> object with no
* recorded error messages.
*
* @param mapping The mapping used to select this instance
* @param request The servlet request we are processing
* @return ActionErrors containing validation errors.
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
logger.info("validate() method called");
ServletContext application = getServlet().getServletContext();
logger.info("ServletContext object created");
ActionErrors errors = new ActionErrors();
logger.info("ActionErrors container created");
// call to the Resources class
Validator validator = Resources.initValidator(mapping.getPath(),
this,
application,
request,
errors,
page);
// validate form using the validation rules
try {
validatorResults = validator.validate();
} catch (ValidatorException e) {
logger.error(e.getMessage(), e);
}
logger.info("-------------------------------- ERROR CHECK");
// create new error container containing the human readable errors
ActionErrors errorContainer = new ActionErrors();
if (!errors.isEmpty() ) {
// there are errors
logger.info("errors found");
Iterator iterProperties = errors.properties();
while (iterProperties.hasNext() ) {
// get ActionError
String property = (String) iterProperties.next();
Iterator actionErrorIter = errors.get(property);
while (actionErrorIter.hasNext() ) {
// get the actionerror object
ActionError actionError = (ActionError)
actionErrorIter.next();
// construct a human readable error message
String errorType = actionError.getKey();
String msg = property + "." + errorType;
logger.info("error " + msg);
// add new ActionError object to error container
errorContainer.add(msg, new ActionError("foo") );
logger.info("add error " + msg);
}
}
}
return errorContainer;
}
--------------------------- END Validate() method's java code sample
-----------------------------------------------
Regards, David.
Bernhard Woehrlin wrote:
>Hi David,
>
>I guess this is not a problem of the DynaValidatorForms. Just check out the
>sample application and you'll experience the same effect: No errors under
>the 'errors' tag - even if you are sure that your form contains some errors.
>
>This is because you type the 'debugxml=true' just in the location field of
>your browser, aren't you? For testing forms add this parameter to the action
>of your form. Just then the server has access to all the necessaray
>parameters in hidden fields and form fields.
>
>Regards,
>
>Berny
>
>
>
>
>>-----Ursprungliche Nachricht-----
>>Von: [email protected]
>>[mailto:[email protected]]Im Auftrag von David
>>Stabel
>>Gesendet: Montag, 7. April 2003 16:13
>>An: [email protected]
>>Betreff: [Strutscx-user] Using DynaValidatorForms in StrutsCX
>>
>>
>>Hello all,
>>
>>I have a question concenring the DynaValidatorForm in StrutsCX.
>>I specified a form with its properties in struts-config.xml and related
>>it to an Action.
>>The form is of type 'org.apache.struts.validator.DynaValidatorForm'.
>>(Using the 'com.cappuccinonet.strutscx.validator.DynaValidatorForm'
>>resulted in no validation at all...?)
>>In validator.xml I have specified for each form property (field) a
>>validation rule.
>>
>>When I invoke the corresponding Action, the form gets validated.
>>Till here there's no problem.....
>>
>>But then the validator catches a field that doesn't satisfy the
>>corresponding validation-rule.
>>I would expect that the returned XML data (catched with 'debugxml=true')
>>contains this error under the 'errors' tag.
>>However there's nothing :(
>>
>>Can somebody tell me what's going wrong....?
>>
>>Thanks, David.
>>--
>>[email protected]
>>
>>
>>
>>-------------------------------------------------------
>>This SF.net email is sponsored by: ValueWeb:
>>Dedicated Hosting for just $79/mo with 500 GB of bandwidth!
>>No other company gives more support or power for your dedicated server
>>http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/
>>_______________________________________________
>>Strutscx-user mailing list
>>[email protected]
>>https://lists.sourceforge.net/lists/listinfo/strutscx-user
>>
>>
>>
-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf