You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Raghu Kanchustambham <kr...@gmail.com> on 2005/11/24 21:30:59 UTC
conditional validation
Hi,
I have the following situation:
1. educationDetails[0].degree.id is not chosen. (drop down return value =
-1).
OR
2. educationDetails[0].degree.id > 0 (the user chose one of the options from
the drop down) which implies the marks should be atleast 35% and not more
than 100%
I tried the attached validation... but here is my dilema...
a. If I choose minimum percentage to be 35 in the validation file .. as
<var-name>min</var-name>
<var-value>35</var-value>
the user will not have the choice to leave it as zero if he doesnt
choose the degree (zero I think is the most appropriate value for marks if
the degree is not chosen)
b. Because of (a) I am forced to leave the lower bound as 0 in the float
range validation. However, using validwhen tag .. when I tried to check 100
>= marks, it is doing a string comparision though the underlying datatypes
are floats. This behavior beats me ... because I know if I provide a
non-number in the field it would get converted to zero by JSP, but why
should the validator do a string comparision and not the int/float
comparisions? Afterall the validator is invoked after the form is
filled...it has the "luxury" to use "typed" comparisions... am i missing
something? Can someone fill in the gaps in my understanding and also suggest
How should I solve this problem in an elegant fashion?
<field property="marks" indexedListProperty="educationDetails"
depends="float,floatRange,validwhen">
<arg name="floatRange" key="${var:min}" resource="false"
position="1" />
<arg name="floatRange" key="${var:max}" resource="false"
position="2" />
<var>
<var-name>min</var-name>
<var-value>0</var-value>
</var>
<var>
<var-name>max</var-name>
<var-value>100</var-value>
</var>
<var>
<var-name>test</var-name>
<var-value>((educationDetails[0].degree.id == -1) or
((educationDetails[0].marks >= 35) and (100 >= educationDetails[0].marks)))
</var-value>
</var>
<arg0 key="Education Details Percentage(s) greater than 35%
and less than 100% " resource="false"/>
</field>
Thanks much.
Regards,
Raghu
Re: conditional validation
Posted by Martin Gainty <mg...@hotmail.com>.
Hello Raghu-
going back to lauries original suggestion in your validation*.xml
following these usual caveats
<li>All comparisons must be enclosed in parens.</li>
<li>Only two items may be joined with <code>and</code> or <code>or</code></li>
<li>If both items to be compared are convertable to ints, a numeric
comparison is done, otherwise a string comparison is done.
</li>
<!-- wild guess as to the fieldname used here.. I called it test.RaghuTest.label-->
<!-- you could spend much time looking for the proper escape mechanism but for now lets use the CDATA shortcut -->
<pre><code><![CDATA[
<field property="RaghuTest" depends="validwhen">
<arg0 key="test.RaghuTest.label"/>
<var>
<var-name>testvar</var-name>
<var-value>(educationDetails[0].marks <= 100)</var-value>
</var>
</field>
]]></code></pre>
Is it my undertsanding that this validation field in your validation*.xml does not work?
M-
----- Original Message -----
From: "Raghu Kanchustambham" <kr...@gmail.com>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Sunday, November 27, 2005 1:32 PM
Subject: Re: conditional validation
Thanks Martin for responding. But I am afraid we probably are talking of two
different things. Unless I am missing some point somewhere. I am referring
to regular expressions in the context of validator framework. (entries in
validation.xml).
The tags that you provided a reference to, are useful in the JSPs to do
comparisions. I am not too sure if I can use these tags in the
validation.xml.
Thanks & Best Regards,
Raghu
On 11/27/05, Martin Gainty <mg...@hotmail.com> wrote:
>
> Raghu-
> Have you looked at implementing with conditional struts-logic lessEqual
> Taglib?
>
> http://struts.apache.org/struts-doc-1.2.7/userGuide/struts-logic.html#lessEqual
> HTH,
> M-
> ----- Original Message -----
> From: "Raghu Kanchustambham" <kr...@gmail.com>
> To: "Struts Users Mailing List" <us...@struts.apache.org>
> Sent: Sunday, November 27, 2005 3:43 AM
> Subject: Re: conditional validation
>
>
> > ...(educationDetails[0].marks le 100)...
>
> Doesnt work... le/ge/gt/lt is not recognized.
>
> I worked around the problem by enforcing through floatRange to be between
> 35
> and 100. Note that floatRange validations work perfectly fine. I wanted to
> allow either a "0" (to stand for a 'dont care') and a range between 35 and
> 100. This condition necessarily meant I had to write regular expressions
> ...
> that way I thought I could avoid the need for "String" counterparts of
> these
> parameters for validation to work on them. (Strings get converted to a 0
> even before the validator acts on them).
>
> Now I allow a blank for dont care and the number (if entered) has to be
> between 35 and 100. I can now use the floatRange for this but as a
> side-effect I had to introduce the "String" counterpart on which
> floatRange would work and also would let you have a blank input (which now
> doesnt get converted to a 0). But the benefit of this is that you dont
> "loose" the "wrong input" that user entered (as Laurie suggested in the
> previous post).
>
> I was wondering if it is possible to use validwhen in conjunction with
> another validation. I should be able to say "validwhen" through some
> expression followed by "floatRange" validation etc... currently I am
> using regular expressions to mimic the floatRange when used on a
> conditional
> basis. If it doesnt exist already, is it very difficult to build it?
>
> Thanks.
> Regards,
> Raghu
>
>
>
>
>
>
>
>
> On 11/26/05, Laurie Harper <la...@holoweb.net> wrote:
> >
> > Raghu Kanchustambham wrote:
> > >> Second, shouldn't you allow an empty value for marks if degree.idisn't
> > > set,
> > >> rather than forcing the user to enter numeric data that's going to be
> > >> ignored?
> > >
> > > I really dont care about what the user enters for marks if he chooses
> > "none"
> > > for the degree. I dont care even if he enters a alpha-numeric ..
> because
> > > that would get converted to a zero before the validator acts on it.
> >
> > That is true assuming your form property is of type float, not String.
> > The disadvantage of doing that is that if the user enters non-numerical
> > data you can't redisplay it after failing validation. If you don't care
> > about being able to redisplay invalid data for the user to correct, you
> > can get away with using a float-typed property and not worry about this.
> >
> > >> What makes you think the validator is doing a string rather than
> > numeric
> > > comparison?
> > >
> > > Lets do a small change to illustrate that it is indeed doing a string
> > > comparision....(may be its doing something else but not numeric
> > comparision!
> > > )
> > >
> > > Reconsider the following (with changed values just to illustrate the
> > point):
> > >
> > > I changed the max value to 500 from 100 and I have removed the
> redundant
> > > "100>=" check as you pointed out. When I choose the degree and enter
> 36
> > for
> > > marks.. there is no validation error (which is expected). However,
> when
> > I
> > > enter 123, which is numerically greater than 35 but "stringically"
> > lesser
> > > than 35, then it throws up a validation error. Similarly 349 fails
> the
> > > validation but 351 passes it! Similarly 7 passes the validation test
> > inspite
> > > of it being less than 35 numerically !
> > >
> > > What I see is that when I enter 65 which is "stringically" greater
> than
> > 500
> > > ... it passes the validation as if to suggest it is a numerical
> > comparision
> > > indeed! But note that this check is being performed by the floatRange
> > > validation and not the validwhen rule. So I guess floatRange and
> > intRange
> > > work as expected but validwhen does a string comparision. In theory
> (at
> > > least) I dont see any reason why validwhen cant do a more semantically
> > > correct comparision.
> >
> > OK, so it does look like you're getting a string comparison. Was my
> > assumption right above that your form property's type is float? I would
> > think this was a bug (I would expect a comparison between an unquoted
> > number and a property of numeric type to work as you're expecting).
> >
> > Try this as an alternative (untested):
> >
> > ...(educationDetails[0].marks le 100)...
> >
> > > And on a side note.. cant I specify a "lesser than" operation in my
> > checks
> > > ... should i always have to work around by swapping the numbers and
> > convert
> > > the expression to "greater than" ... bcoz my IDE seems to be
> complaining
> > abt
> > > the DTD mismatch when I try the "lesser than" operation. I hope I am
> > using
> > > the right DTD:
> > >
> > > <!DOCTYPE form-validation PUBLIC
> > > "-//Apache Software Foundation//DTD Commons Validator Rules
> > > Configuration 1.1.3//EN"
> > > "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd
> ">
> >
> > This isn't a DTD issue, it's an XML syntax issue. Remember that '<' is a
> > special character in XML. The write the litteral character '<' you have
> > to use an entity escape (usually '<'). As an alternative, validator
> > also recognizes the textual operators ('lt', 'gt', etc.) which may be
> > easier to read/write.
> >
> > L.
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>
Re: conditional validation
Posted by Raghu Kanchustambham <kr...@gmail.com>.
Thanks Martin for responding. But I am afraid we probably are talking of two
different things. Unless I am missing some point somewhere. I am referring
to regular expressions in the context of validator framework. (entries in
validation.xml).
The tags that you provided a reference to, are useful in the JSPs to do
comparisions. I am not too sure if I can use these tags in the
validation.xml.
Thanks & Best Regards,
Raghu
On 11/27/05, Martin Gainty <mg...@hotmail.com> wrote:
>
> Raghu-
> Have you looked at implementing with conditional struts-logic lessEqual
> Taglib?
>
> http://struts.apache.org/struts-doc-1.2.7/userGuide/struts-logic.html#lessEqual
> HTH,
> M-
> ----- Original Message -----
> From: "Raghu Kanchustambham" <kr...@gmail.com>
> To: "Struts Users Mailing List" <us...@struts.apache.org>
> Sent: Sunday, November 27, 2005 3:43 AM
> Subject: Re: conditional validation
>
>
> > ...(educationDetails[0].marks le 100)...
>
> Doesnt work... le/ge/gt/lt is not recognized.
>
> I worked around the problem by enforcing through floatRange to be between
> 35
> and 100. Note that floatRange validations work perfectly fine. I wanted to
> allow either a "0" (to stand for a 'dont care') and a range between 35 and
> 100. This condition necessarily meant I had to write regular expressions
> ...
> that way I thought I could avoid the need for "String" counterparts of
> these
> parameters for validation to work on them. (Strings get converted to a 0
> even before the validator acts on them).
>
> Now I allow a blank for dont care and the number (if entered) has to be
> between 35 and 100. I can now use the floatRange for this but as a
> side-effect I had to introduce the "String" counterpart on which
> floatRange would work and also would let you have a blank input (which now
> doesnt get converted to a 0). But the benefit of this is that you dont
> "loose" the "wrong input" that user entered (as Laurie suggested in the
> previous post).
>
> I was wondering if it is possible to use validwhen in conjunction with
> another validation. I should be able to say "validwhen" through some
> expression followed by "floatRange" validation etc... currently I am
> using regular expressions to mimic the floatRange when used on a
> conditional
> basis. If it doesnt exist already, is it very difficult to build it?
>
> Thanks.
> Regards,
> Raghu
>
>
>
>
>
>
>
>
> On 11/26/05, Laurie Harper <la...@holoweb.net> wrote:
> >
> > Raghu Kanchustambham wrote:
> > >> Second, shouldn't you allow an empty value for marks if degree.idisn't
> > > set,
> > >> rather than forcing the user to enter numeric data that's going to be
> > >> ignored?
> > >
> > > I really dont care about what the user enters for marks if he chooses
> > "none"
> > > for the degree. I dont care even if he enters a alpha-numeric ..
> because
> > > that would get converted to a zero before the validator acts on it.
> >
> > That is true assuming your form property is of type float, not String.
> > The disadvantage of doing that is that if the user enters non-numerical
> > data you can't redisplay it after failing validation. If you don't care
> > about being able to redisplay invalid data for the user to correct, you
> > can get away with using a float-typed property and not worry about this.
> >
> > >> What makes you think the validator is doing a string rather than
> > numeric
> > > comparison?
> > >
> > > Lets do a small change to illustrate that it is indeed doing a string
> > > comparision....(may be its doing something else but not numeric
> > comparision!
> > > )
> > >
> > > Reconsider the following (with changed values just to illustrate the
> > point):
> > >
> > > I changed the max value to 500 from 100 and I have removed the
> redundant
> > > "100>=" check as you pointed out. When I choose the degree and enter
> 36
> > for
> > > marks.. there is no validation error (which is expected). However,
> when
> > I
> > > enter 123, which is numerically greater than 35 but "stringically"
> > lesser
> > > than 35, then it throws up a validation error. Similarly 349 fails
> the
> > > validation but 351 passes it! Similarly 7 passes the validation test
> > inspite
> > > of it being less than 35 numerically !
> > >
> > > What I see is that when I enter 65 which is "stringically" greater
> than
> > 500
> > > ... it passes the validation as if to suggest it is a numerical
> > comparision
> > > indeed! But note that this check is being performed by the floatRange
> > > validation and not the validwhen rule. So I guess floatRange and
> > intRange
> > > work as expected but validwhen does a string comparision. In theory
> (at
> > > least) I dont see any reason why validwhen cant do a more semantically
> > > correct comparision.
> >
> > OK, so it does look like you're getting a string comparison. Was my
> > assumption right above that your form property's type is float? I would
> > think this was a bug (I would expect a comparison between an unquoted
> > number and a property of numeric type to work as you're expecting).
> >
> > Try this as an alternative (untested):
> >
> > ...(educationDetails[0].marks le 100)...
> >
> > > And on a side note.. cant I specify a "lesser than" operation in my
> > checks
> > > ... should i always have to work around by swapping the numbers and
> > convert
> > > the expression to "greater than" ... bcoz my IDE seems to be
> complaining
> > abt
> > > the DTD mismatch when I try the "lesser than" operation. I hope I am
> > using
> > > the right DTD:
> > >
> > > <!DOCTYPE form-validation PUBLIC
> > > "-//Apache Software Foundation//DTD Commons Validator Rules
> > > Configuration 1.1.3//EN"
> > > "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd
> ">
> >
> > This isn't a DTD issue, it's an XML syntax issue. Remember that '<' is a
> > special character in XML. The write the litteral character '<' you have
> > to use an entity escape (usually '<'). As an alternative, validator
> > also recognizes the textual operators ('lt', 'gt', etc.) which may be
> > easier to read/write.
> >
> > L.
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>
Re: conditional validation
Posted by Martin Gainty <mg...@hotmail.com>.
Raghu-
Have you looked at implementing with conditional struts-logic lessEqual Taglib?
http://struts.apache.org/struts-doc-1.2.7/userGuide/struts-logic.html#lessEqual
HTH,
M-
----- Original Message -----
From: "Raghu Kanchustambham" <kr...@gmail.com>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Sunday, November 27, 2005 3:43 AM
Subject: Re: conditional validation
> ...(educationDetails[0].marks le 100)...
Doesnt work... le/ge/gt/lt is not recognized.
I worked around the problem by enforcing through floatRange to be between 35
and 100. Note that floatRange validations work perfectly fine. I wanted to
allow either a "0" (to stand for a 'dont care') and a range between 35 and
100. This condition necessarily meant I had to write regular expressions ...
that way I thought I could avoid the need for "String" counterparts of these
parameters for validation to work on them. (Strings get converted to a 0
even before the validator acts on them).
Now I allow a blank for dont care and the number (if entered) has to be
between 35 and 100. I can now use the floatRange for this but as a
side-effect I had to introduce the "String" counterpart on which
floatRange would work and also would let you have a blank input (which now
doesnt get converted to a 0). But the benefit of this is that you dont
"loose" the "wrong input" that user entered (as Laurie suggested in the
previous post).
I was wondering if it is possible to use validwhen in conjunction with
another validation. I should be able to say "validwhen" through some
expression followed by "floatRange" validation etc... currently I am
using regular expressions to mimic the floatRange when used on a conditional
basis. If it doesnt exist already, is it very difficult to build it?
Thanks.
Regards,
Raghu
On 11/26/05, Laurie Harper <la...@holoweb.net> wrote:
>
> Raghu Kanchustambham wrote:
> >> Second, shouldn't you allow an empty value for marks if degree.id isn't
> > set,
> >> rather than forcing the user to enter numeric data that's going to be
> >> ignored?
> >
> > I really dont care about what the user enters for marks if he chooses
> "none"
> > for the degree. I dont care even if he enters a alpha-numeric .. because
> > that would get converted to a zero before the validator acts on it.
>
> That is true assuming your form property is of type float, not String.
> The disadvantage of doing that is that if the user enters non-numerical
> data you can't redisplay it after failing validation. If you don't care
> about being able to redisplay invalid data for the user to correct, you
> can get away with using a float-typed property and not worry about this.
>
> >> What makes you think the validator is doing a string rather than
> numeric
> > comparison?
> >
> > Lets do a small change to illustrate that it is indeed doing a string
> > comparision....(may be its doing something else but not numeric
> comparision!
> > )
> >
> > Reconsider the following (with changed values just to illustrate the
> point):
> >
> > I changed the max value to 500 from 100 and I have removed the redundant
> > "100>=" check as you pointed out. When I choose the degree and enter 36
> for
> > marks.. there is no validation error (which is expected). However, when
> I
> > enter 123, which is numerically greater than 35 but "stringically"
> lesser
> > than 35, then it throws up a validation error. Similarly 349 fails the
> > validation but 351 passes it! Similarly 7 passes the validation test
> inspite
> > of it being less than 35 numerically !
> >
> > What I see is that when I enter 65 which is "stringically" greater than
> 500
> > ... it passes the validation as if to suggest it is a numerical
> comparision
> > indeed! But note that this check is being performed by the floatRange
> > validation and not the validwhen rule. So I guess floatRange and
> intRange
> > work as expected but validwhen does a string comparision. In theory (at
> > least) I dont see any reason why validwhen cant do a more semantically
> > correct comparision.
>
> OK, so it does look like you're getting a string comparison. Was my
> assumption right above that your form property's type is float? I would
> think this was a bug (I would expect a comparison between an unquoted
> number and a property of numeric type to work as you're expecting).
>
> Try this as an alternative (untested):
>
> ...(educationDetails[0].marks le 100)...
>
> > And on a side note.. cant I specify a "lesser than" operation in my
> checks
> > ... should i always have to work around by swapping the numbers and
> convert
> > the expression to "greater than" ... bcoz my IDE seems to be complaining
> abt
> > the DTD mismatch when I try the "lesser than" operation. I hope I am
> using
> > the right DTD:
> >
> > <!DOCTYPE form-validation PUBLIC
> > "-//Apache Software Foundation//DTD Commons Validator Rules
> > Configuration 1.1.3//EN"
> > "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
>
> This isn't a DTD issue, it's an XML syntax issue. Remember that '<' is a
> special character in XML. The write the litteral character '<' you have
> to use an entity escape (usually '<'). As an alternative, validator
> also recognizes the textual operators ('lt', 'gt', etc.) which may be
> easier to read/write.
>
> L.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
Re: conditional validation
Posted by Raghu Kanchustambham <kr...@gmail.com>.
> ...(educationDetails[0].marks le 100)...
Doesnt work... le/ge/gt/lt is not recognized.
I worked around the problem by enforcing through floatRange to be between 35
and 100. Note that floatRange validations work perfectly fine. I wanted to
allow either a "0" (to stand for a 'dont care') and a range between 35 and
100. This condition necessarily meant I had to write regular expressions ...
that way I thought I could avoid the need for "String" counterparts of these
parameters for validation to work on them. (Strings get converted to a 0
even before the validator acts on them).
Now I allow a blank for dont care and the number (if entered) has to be
between 35 and 100. I can now use the floatRange for this but as a
side-effect I had to introduce the "String" counterpart on which
floatRange would work and also would let you have a blank input (which now
doesnt get converted to a 0). But the benefit of this is that you dont
"loose" the "wrong input" that user entered (as Laurie suggested in the
previous post).
I was wondering if it is possible to use validwhen in conjunction with
another validation. I should be able to say "validwhen" through some
expression followed by "floatRange" validation etc... currently I am
using regular expressions to mimic the floatRange when used on a conditional
basis. If it doesnt exist already, is it very difficult to build it?
Thanks.
Regards,
Raghu
On 11/26/05, Laurie Harper <la...@holoweb.net> wrote:
>
> Raghu Kanchustambham wrote:
> >> Second, shouldn't you allow an empty value for marks if degree.id isn't
> > set,
> >> rather than forcing the user to enter numeric data that's going to be
> >> ignored?
> >
> > I really dont care about what the user enters for marks if he chooses
> "none"
> > for the degree. I dont care even if he enters a alpha-numeric .. because
> > that would get converted to a zero before the validator acts on it.
>
> That is true assuming your form property is of type float, not String.
> The disadvantage of doing that is that if the user enters non-numerical
> data you can't redisplay it after failing validation. If you don't care
> about being able to redisplay invalid data for the user to correct, you
> can get away with using a float-typed property and not worry about this.
>
> >> What makes you think the validator is doing a string rather than
> numeric
> > comparison?
> >
> > Lets do a small change to illustrate that it is indeed doing a string
> > comparision....(may be its doing something else but not numeric
> comparision!
> > )
> >
> > Reconsider the following (with changed values just to illustrate the
> point):
> >
> > I changed the max value to 500 from 100 and I have removed the redundant
> > "100>=" check as you pointed out. When I choose the degree and enter 36
> for
> > marks.. there is no validation error (which is expected). However, when
> I
> > enter 123, which is numerically greater than 35 but "stringically"
> lesser
> > than 35, then it throws up a validation error. Similarly 349 fails the
> > validation but 351 passes it! Similarly 7 passes the validation test
> inspite
> > of it being less than 35 numerically !
> >
> > What I see is that when I enter 65 which is "stringically" greater than
> 500
> > ... it passes the validation as if to suggest it is a numerical
> comparision
> > indeed! But note that this check is being performed by the floatRange
> > validation and not the validwhen rule. So I guess floatRange and
> intRange
> > work as expected but validwhen does a string comparision. In theory (at
> > least) I dont see any reason why validwhen cant do a more semantically
> > correct comparision.
>
> OK, so it does look like you're getting a string comparison. Was my
> assumption right above that your form property's type is float? I would
> think this was a bug (I would expect a comparison between an unquoted
> number and a property of numeric type to work as you're expecting).
>
> Try this as an alternative (untested):
>
> ...(educationDetails[0].marks le 100)...
>
> > And on a side note.. cant I specify a "lesser than" operation in my
> checks
> > ... should i always have to work around by swapping the numbers and
> convert
> > the expression to "greater than" ... bcoz my IDE seems to be complaining
> abt
> > the DTD mismatch when I try the "lesser than" operation. I hope I am
> using
> > the right DTD:
> >
> > <!DOCTYPE form-validation PUBLIC
> > "-//Apache Software Foundation//DTD Commons Validator Rules
> > Configuration 1.1.3//EN"
> > "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
>
> This isn't a DTD issue, it's an XML syntax issue. Remember that '<' is a
> special character in XML. The write the litteral character '<' you have
> to use an entity escape (usually '<'). As an alternative, validator
> also recognizes the textual operators ('lt', 'gt', etc.) which may be
> easier to read/write.
>
> L.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
Re: conditional validation
Posted by Laurie Harper <la...@holoweb.net>.
Raghu Kanchustambham wrote:
>> Second, shouldn't you allow an empty value for marks if degree.id isn't
> set,
>> rather than forcing the user to enter numeric data that's going to be
>> ignored?
>
> I really dont care about what the user enters for marks if he chooses "none"
> for the degree. I dont care even if he enters a alpha-numeric .. because
> that would get converted to a zero before the validator acts on it.
That is true assuming your form property is of type float, not String.
The disadvantage of doing that is that if the user enters non-numerical
data you can't redisplay it after failing validation. If you don't care
about being able to redisplay invalid data for the user to correct, you
can get away with using a float-typed property and not worry about this.
>> What makes you think the validator is doing a string rather than numeric
> comparison?
>
> Lets do a small change to illustrate that it is indeed doing a string
> comparision....(may be its doing something else but not numeric comparision!
> )
>
> Reconsider the following (with changed values just to illustrate the point):
>
> I changed the max value to 500 from 100 and I have removed the redundant
> "100>=" check as you pointed out. When I choose the degree and enter 36 for
> marks.. there is no validation error (which is expected). However, when I
> enter 123, which is numerically greater than 35 but "stringically" lesser
> than 35, then it throws up a validation error. Similarly 349 fails the
> validation but 351 passes it! Similarly 7 passes the validation test inspite
> of it being less than 35 numerically !
>
> What I see is that when I enter 65 which is "stringically" greater than 500
> ... it passes the validation as if to suggest it is a numerical comparision
> indeed! But note that this check is being performed by the floatRange
> validation and not the validwhen rule. So I guess floatRange and intRange
> work as expected but validwhen does a string comparision. In theory (at
> least) I dont see any reason why validwhen cant do a more semantically
> correct comparision.
OK, so it does look like you're getting a string comparison. Was my
assumption right above that your form property's type is float? I would
think this was a bug (I would expect a comparison between an unquoted
number and a property of numeric type to work as you're expecting).
Try this as an alternative (untested):
...(educationDetails[0].marks le 100)...
> And on a side note.. cant I specify a "lesser than" operation in my checks
> ... should i always have to work around by swapping the numbers and convert
> the expression to "greater than" ... bcoz my IDE seems to be complaining abt
> the DTD mismatch when I try the "lesser than" operation. I hope I am using
> the right DTD:
>
> <!DOCTYPE form-validation PUBLIC
> "-//Apache Software Foundation//DTD Commons Validator Rules
> Configuration 1.1.3//EN"
> "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
This isn't a DTD issue, it's an XML syntax issue. Remember that '<' is a
special character in XML. The write the litteral character '<' you have
to use an entity escape (usually '<'). As an alternative, validator
also recognizes the textual operators ('lt', 'gt', etc.) which may be
easier to read/write.
L.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: conditional validation
Posted by Raghu Kanchustambham <kr...@gmail.com>.
Hi Laurie,
Thanks for responding.
> Firstly, the check for marks <= 100 in the validwhen rule is redundant,
True! I realize that and have removed that.
> Second, shouldn't you allow an empty value for marks if degree.id isn't
set,
> rather than forcing the user to enter numeric data that's going to be
> ignored?
I really dont care about what the user enters for marks if he chooses "none"
for the degree. I dont care even if he enters a alpha-numeric .. because
that would get converted to a zero before the validator acts on it.
>What makes you think the validator is doing a string rather than numeric
comparison?
Lets do a small change to illustrate that it is indeed doing a string
comparision....(may be its doing something else but not numeric comparision!
)
Reconsider the following (with changed values just to illustrate the point):
I changed the max value to 500 from 100 and I have removed the redundant
"100>=" check as you pointed out. When I choose the degree and enter 36 for
marks.. there is no validation error (which is expected). However, when I
enter 123, which is numerically greater than 35 but "stringically" lesser
than 35, then it throws up a validation error. Similarly 349 fails the
validation but 351 passes it! Similarly 7 passes the validation test inspite
of it being less than 35 numerically !
What I see is that when I enter 65 which is "stringically" greater than 500
... it passes the validation as if to suggest it is a numerical comparision
indeed! But note that this check is being performed by the floatRange
validation and not the validwhen rule. So I guess floatRange and intRange
work as expected but validwhen does a string comparision. In theory (at
least) I dont see any reason why validwhen cant do a more semantically
correct comparision.
And on a side note.. cant I specify a "lesser than" operation in my checks
... should i always have to work around by swapping the numbers and convert
the expression to "greater than" ... bcoz my IDE seems to be complaining abt
the DTD mismatch when I try the "lesser than" operation. I hope I am using
the right DTD:
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules
Configuration 1.1.3//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
Any light ?
Thanks.
Best Regards,
Raghu
----
<field property="marks" indexedListProperty="educationDetails"
depends="float,floatRange,validwhen">
<arg name="floatRange" key="${var:min}" resource="false"
position="1" />
<arg name="floatRange" key="${var:max}" resource="false"
position="2" />
<var>
<var-name>min</var-name>
<var-value>0</var-value>
</var>
<var>
<var-name>max</var-name>
<var-value>500</var-value>
</var>
<var>
<var-name>test</var-name>
<var-value>((educationDetails[0].degree.id == -1) or
(educationDetails[0].marks >= 35)) </var-value>
</var>
<arg0 key="Education Details Percentage(s) greater than 35%
and less than 500% " resource="false"/>
</field>
-----
On 11/25/05, Laurie Harper <la...@holoweb.net> wrote:
>
> Raghu Kanchustambham wrote:
> > Hi,
> > I have the following situation:
> > 1. educationDetails[0].degree.id is not chosen. (drop down return value
> =
> > -1).
> >
> > OR
> >
> > 2. educationDetails[0].degree.id > 0 (the user chose one of the options
> from
> > the drop down) which implies the marks should be atleast 35% and not
> more
> > than 100%
> >
> > I tried the attached validation... but here is my dilema...
> >
> > a. If I choose minimum percentage to be 35 in the validation file .. as
> > <var-name>min</var-name>
> > <var-value>35</var-value>
> > the user will not have the choice to leave it as zero if he doesnt
> > choose the degree (zero I think is the most appropriate value for marks
> if
> > the degree is not chosen)
> >
> >
> > b. Because of (a) I am forced to leave the lower bound as 0 in the float
> > range validation. However, using validwhen tag .. when I tried to check
> 100
> >> = marks, it is doing a string comparision though the underlying
> datatypes
> > are floats. This behavior beats me ... because I know if I provide a
> > non-number in the field it would get converted to zero by JSP, but why
> > should the validator do a string comparision and not the int/float
> > comparisions? Afterall the validator is invoked after the form is
> > filled...it has the "luxury" to use "typed" comparisions... am i missing
> > something? Can someone fill in the gaps in my understanding and also
> suggest
> > How should I solve this problem in an elegant fashion?
> >
> >
> > <field property="marks" indexedListProperty="educationDetails"
> > depends="float,floatRange,validwhen">
> > <arg name="floatRange" key="${var:min}" resource="false"
> > position="1" />
> > <arg name="floatRange" key="${var:max}" resource="false"
> > position="2" />
> > <var>
> > <var-name>min</var-name>
> > <var-value>0</var-value>
> > </var>
> > <var>
> > <var-name>max</var-name>
> > <var-value>100</var-value>
> > </var>
> > <var>
> > <var-name>test</var-name>
> > <var-value>((educationDetails[0].degree.id == -1)
> or
> > ((educationDetails[0].marks >= 35) and (100 >=
> educationDetails[0].marks)))
> > </var-value>
> > </var>
> > <arg0 key="Education Details Percentage(s) greater than
> 35%
> > and less than 100% " resource="false"/>
> > </field>
> >
> >
> > Thanks much.
> > Regards,
> > Raghu
>
> Firstly, the check for marks <= 100 in the validwhen rule is redundant,
> since that's alredy being checked by the floatRange check. Second,
> shouldn't you allow an empty value for marks if degree.id isn't set,
> rather than forcing the user to enter numeric data that's going to be
> ignored?
>
> That said, I'm not sure why the check as you have written it wouldn't
> work. What makes you think the validator is doing a string rather than
> numeric comparison? And why for the <=100 check but not the >=35 check?
> If that's really the case, you could try swapping the operands to put
> the variable reference on the left hand side (i.e. write the check in
> the same order as the >=35 check).
>
> L.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
Re: conditional validation
Posted by Laurie Harper <la...@holoweb.net>.
Raghu Kanchustambham wrote:
> Hi,
> I have the following situation:
> 1. educationDetails[0].degree.id is not chosen. (drop down return value =
> -1).
>
> OR
>
> 2. educationDetails[0].degree.id > 0 (the user chose one of the options from
> the drop down) which implies the marks should be atleast 35% and not more
> than 100%
>
> I tried the attached validation... but here is my dilema...
>
> a. If I choose minimum percentage to be 35 in the validation file .. as
> <var-name>min</var-name>
> <var-value>35</var-value>
> the user will not have the choice to leave it as zero if he doesnt
> choose the degree (zero I think is the most appropriate value for marks if
> the degree is not chosen)
>
>
> b. Because of (a) I am forced to leave the lower bound as 0 in the float
> range validation. However, using validwhen tag .. when I tried to check 100
>> = marks, it is doing a string comparision though the underlying datatypes
> are floats. This behavior beats me ... because I know if I provide a
> non-number in the field it would get converted to zero by JSP, but why
> should the validator do a string comparision and not the int/float
> comparisions? Afterall the validator is invoked after the form is
> filled...it has the "luxury" to use "typed" comparisions... am i missing
> something? Can someone fill in the gaps in my understanding and also suggest
> How should I solve this problem in an elegant fashion?
>
>
> <field property="marks" indexedListProperty="educationDetails"
> depends="float,floatRange,validwhen">
> <arg name="floatRange" key="${var:min}" resource="false"
> position="1" />
> <arg name="floatRange" key="${var:max}" resource="false"
> position="2" />
> <var>
> <var-name>min</var-name>
> <var-value>0</var-value>
> </var>
> <var>
> <var-name>max</var-name>
> <var-value>100</var-value>
> </var>
> <var>
> <var-name>test</var-name>
> <var-value>((educationDetails[0].degree.id == -1) or
> ((educationDetails[0].marks >= 35) and (100 >= educationDetails[0].marks)))
> </var-value>
> </var>
> <arg0 key="Education Details Percentage(s) greater than 35%
> and less than 100% " resource="false"/>
> </field>
>
>
> Thanks much.
> Regards,
> Raghu
Firstly, the check for marks <= 100 in the validwhen rule is redundant,
since that's alredy being checked by the floatRange check. Second,
shouldn't you allow an empty value for marks if degree.id isn't set,
rather than forcing the user to enter numeric data that's going to be
ignored?
That said, I'm not sure why the check as you have written it wouldn't
work. What makes you think the validator is doing a string rather than
numeric comparison? And why for the <=100 check but not the >=35 check?
If that's really the case, you could try swapping the operands to put
the variable reference on the left hand side (i.e. write the check in
the same order as the >=35 check).
L.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org