Issue Details (XML | Word | Printable)

Key: STR-2321
Type: Improvement Improvement
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Paul Benedict
Reporter: Nate Minshew
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Struts 1

Validator validwhen comparisons of doubles

Created: 29/Dec/04 09:37 PM   Updated: 26/Aug/07 11:44 PM
Component/s: Core
Affects Version/s: 1.2.2, Nightly Build
Fix Version/s: 1.4.0

File Attachments:
  Size
Java Source File ValidWhenParser.java 2004-12-29 09:46 PM Nate Minshew 15 kB
Environment:
Operating System: Windows XP
Platform: PC
Issue Links:
Duplicate
 
Reference
 

Bugzilla Id: 32875


 Description  « Hide
I ran into a problem this week where validwhen wasn't working correctly when
trying to compare 2 doubles. I have 2 fields, one a minimum value and the other
a maximum value. I setup in the validation.xml this validator dependency:

<field property="sizeHeightMin" depends="required, double" >
    <arg0 key="displayName.sizeHeightMin" />
    <msg name="double" key="errors.numeric" />
</field>
<field property="sizeHeightMax" depends="required, double, validwhen" >
    <arg0 key="displayName.sizeHeightMax" />
    <arg1 key="displayName.sizeHeightMin" />
    <msg name="double" key="errors.numeric" />
    <msg name="validwhen" key="errors.range" />
    <var>
        <var-name>test</var-name>
        <var-value>(*this* > sizeHeightMin)</var-value>
    </var>
</field>

But when I would enter 6.5 for the min and 11.5 for the max it would fail
validation saying that the max was less than the min. After debugging and
tracking down the problem I discovered that the evaluateComparison(Object,
Object, Object) method in ValidWhenParser.class was only checking to see if the
values were integers and if not doing a string comparison, thus the reason I was
getting the validation failure. Also in that method it does a check to see if
the objects passed in are of type Integer.class. Unless I'm missing something I
don't believe that will ever happen since in ValidWhen.class and in the field
method of ValidWhenParser.class you retrieve the value with
ValidatorUtils.getValueAsString(bean, property), thus the value will always be a
string. I have added a patch for this issue and will be sending it in.

 All   Comments   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Nate Minshew added a comment - 29/Dec/04 09:46 PM
Created an attachment (id=13854)
Corrects issue when trying to compare doubles.

I have added support for comparing doubles when using validwhen. I have tested
it and it works correctly now in my application. I noticed that before it did
a check to see if the values from the form are of type Integer.class, but after
reviewing the code it seems that this case can never occur. Everywhere where
the value of the property from the form is retrieved
ValidatorUtils.getValueAsString(bean, property) is used, thus the value should
always be a string. I went ahead and left the code to check for objects of
type Integer.class and now Double.class, but if they are unneeded they could be
removed.

thanks.

Niall Pemberton added a comment - 29/Mar/05 01:20 PM
ValidWhenParser.java is a generated by antlr.

Any changes need to be made to the ValidWhenParser.g file and the
ValidWhenParser.java re-generated. Patches are preferred in "diff" format.

Niall

Niall Pemberton added a comment - 23/Apr/05 12:25 PM
Changing this to an enhancement request.

Niall Pemberton added a comment - 10/Jan/06 04:29 AM
Changing the product to Struts - validwhen is not part of Commons Validator

Henri Yandell added a comment - 02/Feb/07 10:47 PM
Makes sense. The ValidWhenParser defines a DECIMAL_LITERAL to be:

DECIMAL_LITERAL : ('-')? ('1'..'9') ('0'..'9')*;

Which doesn't support decimals (despite the name). Making it support decimals appears to be trickier than just adding the pattern for a decimal format - you have to split DECIMAL_LITERAL and INTEGER_LITERAL so the right types of objects are made, and then you need to find a way to stop the DECIMAL, INTEGER clashing with the INTEGER_LITERAL, OCTAL_LITERAL and IDENTIFIER. Way beyond my Antlr knowledge at the moment, but it does explain why the examples I looked at get complex in this area.

Paul Benedict added a comment - 25/Aug/07 04:55 PM
I propose a fix by instantiating BigDecimal instead of Integer. Therefore, the developer would first run the integer or double validator (ensuring correct type) and then validwhen.