Struts Validator Framework-
hi
Im using struts validator framework for performing validations .
My problem is,
Say i've 2 text fields in my jsp. Im going to perform some validations on these two text fields.Now i've got two buttons (say) but1 & but2, of the type 'submit' . When i submit my form using but1 , i need to validate the two fields. My but2 is for someother purpose ,but it's of the type submit and going to call a method in my action class.
The problem is validator framework is performing the validation on clicking either of these two buttons. I dunn wan to perform any validations wen i click on but2 as i said in above case. Pls make a note that both buttons are going thru the same action mapping and im using DispatchAction ....
Thanx in advance....
# 1
One way could be to use a hidden variable in your form with value true if validation shall be bypassed, change the boolean value on submit with javascript for the one of the two buttons where you want to bypass validation, and in your formBean.validate set all validation inside some test according to the value of the hidden variable
public ActionErrors validate(
ActionMapping mapping, HttpServletRequest request ) {
if("true".equals.getValidationGoAHead()){
validate...
}
# 2
Thanx for suggestion.. but im not going to use validate method in my ActionForm. I m supposed to use validation.xml ........
The problem is if i did it thru validate method.... wat if my ActionForm is used by more than one jsp..and other jsp's does n't require such validations....soo i want validations to be performed specific to my action path........
# 3
a submit button is also an form input element, so it's sended as all the other form input elements.
Suppose you have these two buttons:
<input name="submit1" type="submit" value="submit1">
<input name="submit2" type="submit" value="submit2">
In your struts validator (server side), check what button submit was used:
String submit1 = request.getParameter("submit1");
String submit2 = request.getParameter("submit2");
if(submit1!=null){
//....
}
NB: if you are using FormBean, so add twoextra atributes for the two submit inputs and don't forget to bind them in your jsp. In this case, check twhat submit button was used like the following :
if(yourFormBean.getSubmit1()!=null&&"submit1".equals(yourFormBean.getSubmit1())){
//...
}else if(yourFormBean.getSubmit2()!=null&&"submit2".equals(yourFormBean.getSubmit2())){
//...
}
Hope That Helps
# 5
Well then, define on your struts-config action that you don't want to validate this call
<action
path="/testNoValidate"
name="MyStrutsValidatorActionForm"
type="com.struts.MyAction"
validate = "false"
>
...
</action>