ZF-1769: Zend_Validate_StringLength allows max to be lower than min...

Description

Hi,

I'm using Zend_Validate_StringLength and having a problem. I jumped into the code to see if something was going on in the class that would help explain what I was seeing. Although I haven't gotten to the bottom of that particular problem, while looking over the code I noticed that there is no check when min or max is set to see if min is set to a value higher than max or if max is set to a value lower than min.

While writing this, I did consider the possibilty that there may be value in having min higher than max, but I couldn't think of anything in particular.

I marked this as trivial because I don't think it will be bothering people. BUT, unless there's a good reason not to, it may be a good idea to throw an exception in such circumstances to alert the developer that they're (probably) not using the validator properly....perhaps yielding unexpected results. Or worse yet, perhaps they'll get false positives or false negatives.

I have not actually executed any test, but am pretty sure that the validation will always fail. Throwing an exception when min and max are "backward" may simply save the developer some time troubleshooting.

I hope that's not too confusing. Please feel free to let me know if you have any questions.

Travis

Comments

Hi,

I just realized that my statement, "validation will always fail" is not correct. I think it will fail when min is higher than max and max is not null.

Travis

I agree that it makes most sense to throw an exception when:

  1. the max is set to something less than the min, or
  2. the min is set to something greater than the max (if one has been specified)

This is a slight break to backward compatibility, but I think it is good to make this change, for the reasons that Travis already mentioned.

Resolved for 1.1.0 with SVN r5860. Since it is a minor BC break, I don't request review for merging with 1.0.1, for which code freeze is tomorrow.