You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Madhav Bhargava <un...@gmail.com> on 2006/02/10 11:25:47 UTC
prevent validation of disabled fields on the JSP page
Hi All,
I have a JSP page where on the select of a radio button some controls are
disabled/enabled. The form bean associated with this JSP extends
ValidatorForm. I have created rules in validation-xml for each of these
controls which are there on the screen. One rule that is common to all the
controls is -"required".
Depending on the selection of the radio button different methods in a
DispatchAction class are called. If i need to do server side validations
then how can i prevent the validator framework to validate controls which
have been disabled.
For client side validations i can just do some slight modifications in
validation-rules.xml and get the behavior i want.
For server side validations of this kind i can think of the following:
1. Override the apache class that provides the "required" behavior, then
change validator-rules.xml to have the custom class instead.
2. override the validate method in the form bean and based on the request
attributes get only errors for the controls that are enabled. - Somehow this
method though simple fix does not appeal to me much.
What can be the best way in which this can be acheived.
One more thing:
Does struts provide you a way in which you can check in a form bean whether
a control was disabled. The behavior that i observed is that if the control
is disabled then the value in the form bean is null. Which is also the case
when the user does not enter anything in the control.
Regards,
madhav
--
When I tell the truth, it is not for the sake of convincing those who do not
know it, but for the sake of defending those that do
Problem whith html:optionscollection
Posted by José María Tristán <jt...@sedasolubles.com>.
Hi,
I have a bean form:
import java.util.*;
import org.apache.struts.action.ActionForm;
import org.apache.struts.util.LabelValueBean;
public class CustomSubmitForm extends ActionForm {
private String m_firstName = null ;
private String[] m_countryCode;
private List lCountryCode = new ArrayList();
private List m_countryOptions = null;
public String getFirstName() {
return m_firstName ;
}
public String[] getCountryCode(){
System.out.println("getCountryCode; " + m_countryCode);
return m_countryCode;
}
public void setCountryCode(String[] m_countryCode){
this.m_countryCode = m_countryCode;
}
public Collection getCountryOptions() {
if (m_countryOptions == null) {
m_countryOptions = new Vector(2) ;
m_countryOptions.add(new LabelValueBean("United States","US"))
;
m_countryOptions.add(new LabelValueBean("Germany","DE")) ;
m_countryOptions.add(new LabelValueBean("España","ES"));
}
return m_countryOptions;
}
and a jsp:
<html:form action="submit.do">
ListEdad:
<html:select property="countryCode" multiple="true">
<html:optionsCollection property="countryOptions" />
</html:select>
<br>
<html:submit/>
</html:form>
This is ok, but how i can select per example Germany and United States per
defect?
Thank you.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: prevent validation of disabled fields on the JSP page
Posted by Madhav Bhargava <un...@gmail.com>.
Unfortunately i am using Struts 1.1 and commons-validator 1.0
On 2/10/06, Vladislav Pernin <vl...@openwide.fr> wrote:
> Which version of commons-validator are you using.
>
> I'm using Struts 1.2.8 and commons-validator 1.2.0, it validate only the
> non disabled field.
>
> ------------------------
> Extracted from commons-validator.jar :
> function validateRequired(form) {
> var isValid = true;
> var focusField = null;
> var i = 0;
> var fields = new Array();
>
> oRequired = eval('new ' + retrieveFormName(form) + '_required()');
>
> for (x in oRequired) {
> var field = form[oRequired[x][0]];
>
> if (field == null) {
> fields[i++] = oRequired[x][1];
> isValid=false;
> } else if ((field.type == 'hidden' ||
> field.type == 'text' ||
> field.type == 'textarea' ||
> field.type == 'file' ||
> field.type == 'radio' ||
> field.type == 'checkbox' ||
> field.type == 'select-one' ||
> field.type == 'password') &&
> *field.disabled == false*) {
> ------------------------
>
>
> Vlad
> Madhav Bhargava wrote:
>
> >Hi All,
> >
> >I have a JSP page where on the select of a radio button some controls are
> >disabled/enabled. The form bean associated with this JSP extends
> >ValidatorForm. I have created rules in validation-xml for each of these
> >controls which are there on the screen. One rule that is common to all
> the
> >controls is -"required".
> >
> >Depending on the selection of the radio button different methods in a
> >DispatchAction class are called. If i need to do server side validations
> >then how can i prevent the validator framework to validate controls which
> >have been disabled.
> >
> >For client side validations i can just do some slight modifications in
> >validation-rules.xml and get the behavior i want.
> >
> >For server side validations of this kind i can think of the following:
> >1. Override the apache class that provides the "required" behavior, then
> >change validator-rules.xml to have the custom class instead.
> >2. override the validate method in the form bean and based on the request
> >attributes get only errors for the controls that are enabled. - Somehow
> this
> >method though simple fix does not appeal to me much.
> >
> >What can be the best way in which this can be acheived.
> >
> >One more thing:
> >
> >Does struts provide you a way in which you can check in a form bean
> whether
> >a control was disabled. The behavior that i observed is that if the
> control
> >is disabled then the value in the form bean is null. Which is also the
> case
> >when the user does not enter anything in the control.
> >
> >Regards,
> >madhav
> >
> >
> >
> >--
> >When I tell the truth, it is not for the sake of convincing those who do
> not
> >know it, but for the sake of defending those that do
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
--
When I tell the truth, it is not for the sake of convincing those who do not
know it, but for the sake of defending those that do
Re: prevent validation of disabled fields on the JSP page
Posted by Vladislav Pernin <vl...@openwide.fr>.
Which version of commons-validator are you using.
I'm using Struts 1.2.8 and commons-validator 1.2.0, it validate only the
non disabled field.
------------------------
Extracted from commons-validator.jar :
function validateRequired(form) {
var isValid = true;
var focusField = null;
var i = 0;
var fields = new Array();
oRequired = eval('new ' + retrieveFormName(form) + '_required()');
for (x in oRequired) {
var field = form[oRequired[x][0]];
if (field == null) {
fields[i++] = oRequired[x][1];
isValid=false;
} else if ((field.type == 'hidden' ||
field.type == 'text' ||
field.type == 'textarea' ||
field.type == 'file' ||
field.type == 'radio' ||
field.type == 'checkbox' ||
field.type == 'select-one' ||
field.type == 'password') &&
*field.disabled == false*) {
------------------------
Vlad
Madhav Bhargava wrote:
>Hi All,
>
>I have a JSP page where on the select of a radio button some controls are
>disabled/enabled. The form bean associated with this JSP extends
>ValidatorForm. I have created rules in validation-xml for each of these
>controls which are there on the screen. One rule that is common to all the
>controls is -"required".
>
>Depending on the selection of the radio button different methods in a
>DispatchAction class are called. If i need to do server side validations
>then how can i prevent the validator framework to validate controls which
>have been disabled.
>
>For client side validations i can just do some slight modifications in
>validation-rules.xml and get the behavior i want.
>
>For server side validations of this kind i can think of the following:
>1. Override the apache class that provides the "required" behavior, then
>change validator-rules.xml to have the custom class instead.
>2. override the validate method in the form bean and based on the request
>attributes get only errors for the controls that are enabled. - Somehow this
>method though simple fix does not appeal to me much.
>
>What can be the best way in which this can be acheived.
>
>One more thing:
>
>Does struts provide you a way in which you can check in a form bean whether
>a control was disabled. The behavior that i observed is that if the control
>is disabled then the value in the form bean is null. Which is also the case
>when the user does not enter anything in the control.
>
>Regards,
>madhav
>
>
>
>--
>When I tell the truth, it is not for the sake of convincing those who do not
>know it, but for the sake of defending those that do
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: prevent validation of disabled fields on the JSP page
Posted by Laurie Harper <la...@holoweb.net>.
Madhav Bhargava wrote:
> Hi All,
>
> I have a JSP page where on the select of a radio button some controls are
> disabled/enabled. The form bean associated with this JSP extends
> ValidatorForm. I have created rules in validation-xml for each of these
> controls which are there on the screen. One rule that is common to all the
> controls is -"required".
>
> Depending on the selection of the radio button different methods in a
> DispatchAction class are called. If i need to do server side validations
> then how can i prevent the validator framework to validate controls which
> have been disabled.
>
> For client side validations i can just do some slight modifications in
> validation-rules.xml and get the behavior i want.
>
> For server side validations of this kind i can think of the following:
> 1. Override the apache class that provides the "required" behavior, then
> change validator-rules.xml to have the custom class instead.
> 2. override the validate method in the form bean and based on the request
> attributes get only errors for the controls that are enabled. - Somehow this
> method though simple fix does not appeal to me much.
>
> What can be the best way in which this can be acheived.
>
> One more thing:
>
> Does struts provide you a way in which you can check in a form bean whether
> a control was disabled. The behavior that i observed is that if the control
> is disabled then the value in the form bean is null. Which is also the case
> when the user does not enter anything in the control.
That's correct, and is standard HTML/HTTP behaviour. You can do two
things to meet your requirements:
- write your validation rules with 'validwhen' instead of 'required', if
there's a way you can describe when a field should be required or not in
a validwhen rule
- write separate sets of validation rules for each use case
The latter is easiest if you have different action mapping paths for
each use case. You probably don't since you mentioned DispatchAction.
But you should be able to use the dispatch method param as the control
variable in your validwhen rules, as in
(*this* == null) or (method='some-action-method-that-requires-this)
L.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org