You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Lee Meador <le...@Valtech.com> on 2005/01/27 21:04:59 UTC

[Validator] Multiple entries for same field loses all but one

I have the same field referenced more than once in the same <form> section:
 
<field property="middleDate" depends="required,date,datenotafter">

<arg0 key="middleDate"/>

<arg1 key="contractEndDate"/>

<var><var-name>baseProperty</var-name><var-value>contractEndDate</var-value></var>

</field>

<<field property="middleDate" depends="required,date,datenotbefore">

<arg0 key="middleDate"/>

<arg1 key="contractStartDate"/>

<var><var-name>baseProperty</var-name><var-value>contractStartDate</var-value></var>

</field>

I have two tests for dates being ordered correctly. I want to see that the middle date is between the start and the end dates (inclusive). So I wrote two new tests that do the comparisons and either pass or fail depending on the ordering.

The problem I am seeing is that only one of the tests is being "done". Well, actually, they are both being done but the ValidatorResults object has a Map to hold the results with the key being the field name and after every test the new result is "merged" with the old. "Merge" in this case means "putAll()" is called on the Map and anything with the same name is either ignored or replaced. (I haven't really figured out which.) So the results of one of the tests is thrown out and the other one is preserved. This seems to work if both tests either fail or succeed. If one fails and one succeeds, I would expect the failure to be preserved at all times. If they both fail I guess the multiple failures should be stored inside the ValidatorResult that is in the Map.

If some better sort of merging isn't done in ValidatorResults it could at least detect that it is losing something and throw an exception saying you can't have two <field> entries with the same 'property' value.

Ok ... I may be missing something here? I didn't spend a long time perusing the source but that what it looks like is happening.

There may be a way of working around this. The first thing that pops into my mind is adding another new test that takes property names for two dates to form a range and tests the property against the range. The second thing is to give the 'baseProperty' two different names for the not-before and not-after tests so that both tests could happen in a single <field> section. (You know, with the commas.)

Does anyone see a better solution for me?

Thanks.

-- Lee Meador


Re: [Validator] Multiple entries for same field loses all but one

Posted by Kishore Senji <ks...@gmail.com>.
> There may be a way of working around this. The first thing that pops into my mind is adding another new test that takes property names for two dates to form a range and tests the property against the range. The second thing is to give the 'baseProperty' two different names for the not-before and not-after tests so that both tests could happen in a single <field> section. (You know, with the commas.)
> 
> Does anyone see a better solution for me?

Field's getKey() checks if the key is null, and if so, it retuns the
propety value.

Looking at the setKey on the Field I thought I can declare a
key="someKey" attribute on the field so that the key attribute value
will be used instead of property value.
Unfortunately, the dtd for the field doesn't have a key attribute and
so, even if one declares it on the field as an attribute it doesn't
get copied.

http://jakarta.apache.org/commons/validator/apidocs/org/apache/commons/validator/Field.html#setKey(java.lang.String)

I guess your first work around is probably better than the second one

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org