DynaActionForm and field checking

G

gk

when i use Form Bean i use ActionErrors to validate

example :


public final class HelloForm extends ActionForm {


public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {

ActionErrors errors = new ActionErrors();

if ((person == null) || (person.length() < 1))
errors.add("person", new
ActionError("ch03.hello.no.person.error"));

return errors;
}





But when if i use DynaActionForm how do i validate ?

i DONT want to use Struts validation.xml + validation-rules.xml.

how do i do field checking if there is DynaActionForm ?
 
T

Tim B

gk said:
when i use Form Bean i use ActionErrors to validate
But when if i use DynaActionForm how do i validate ?

i DONT want to use Struts validation.xml + validation-rules.xml.

how do i do field checking if there is DynaActionForm ?

you could do validation in your action
 
G

gk

you mean i could place this code in the Action class ?



public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {

ActionErrors errors = new ActionErrors();

if ((person == null) || (person.length() < 1))
errors.add("person", new
ActionError("ch03.hello.no.person.error"));

return errors;
}




another question : should we use Struts validation.xml ? some people
say its good ...some says it is not. what do you think ?


is this true Struts also can do Client-side validation ?? !! ...how is
that possible ? only javascript (no other language) does client side
validation . How much correct it is ?
 
A

Abhijat Vatsyayan

I would think you can use org.apache.struts.validator.DynaValidatorForm
Abhijat
 
A

Abhijat Vatsyayan

I think I made is sound simpler than it is (I have not used struts for
development). If you do custom processing for your forms, you have to
write custom code. This code has to work with DynaActionForm. So here
are two approaches I can suggest -

* Write a separate subclass of DynaValidatorForm for each validation. I
have a feeling you do not want to do this.

* Subclass DynaValidatorForm, separate out the validation part (probably
use a strategy) and read the name of this Validator class through
configuration. You can add a property "ValidatorClass" - pass the name
of the concrete validator class through the "form-property" element. The
validate method can create the correct Validator class (by reading the
property "ValidatorClass") and delegate the work to this object.

The documentation for DynaValidatorForm says something about "basic
field validation based on an XML file". You might also want to look into
that (or wait for the eight people who know struts to post a reply).
Abhijat
 
T

Tim B

gk said:
you mean i could place this code in the Action class ?



public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {

ActionErrors errors = new ActionErrors();

if ((person == null) || (person.length() < 1))
errors.add("person", new
ActionError("ch03.hello.no.person.error"));

return errors;
}


yes you could do something like that but you would have to call it from the
'execute' method of your action, or just do it in the execute method, like
this:

public final ActionForward execute(
final ActionMapping mapping,
final ActionForm form,
final HttpServletRequest request,
final HttpServletResponse response) throws Exception {

MyFormBeanType myFormBean = (MyFormBeanType)form;

// note use of ActionMessages to avoid using the
// deprecated 'saveErrors(HttpServletRequest, ActionMessages)'
ActionMessages messages = new ActionMessages();

if ((myFormBean.getPerson() == null) || (myFormBean.getPerson().length() <
1)) {
messages.add("person", new
ActionMessage("ch03.hello.no.person.error"));
}

//maybe validate some additional fields here

if(messages.isEmpty()) {
// do normal stuff here
return mapping.findForward("success");
} else {
this.saveErrors(request, messages);
return mapping.findForward("failure");
}
}
another question : should we use Struts validation.xml ? some people
say its good ...some says it is not. what do you think ?

It's ok if you have forms with many fields that don't require complex
validation. I personally don't like it much, one reason being that there
isn't adequate syntax checking of the validation.xml file. If you have an
error in the xml, in some cases the field being validated will always pass
validation, with no other indication that something is wrong.
is this true Struts also can do Client-side validation ?? !! ...how is
that possible ? only javascript (no other language) does client side
validation . How much correct it is ?

The Struts validator uses javascript to do client side validation, and
client side is optional.
I think you could use a good Struts book. Check out "Struts in Action" by
Ted Husted
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top