1 Reply Latest reply on Jul 20, 2012 4:05 PM by drkstr_1

    Spark NumberValidator Unable to Validate Zero?

    bucpatr1 Level 2

      I ran into an issue recently where entering a value of zero into a field validated by a s:NumberValidator would cause validation to fail with a required field error. I didn't have any min value assigned to the validator so I couldn't see any reason the validation should not pass for this case. After a bit of digging into the source I located the culprit in the validate() method of spark.validators.supportClasses.GlobalizationValidatorBase. I was passing a Number as the parameter of the validate() method rather than binding to a source and property. When it executed

      if (!value)
                  value = getValueFromSource();
      

       

      !value would evalueate to true since value was 0. Since there was no source specified value would get set to null and the 'required' validation would fail.

       

      I realize there are lots of ways I could work around this, but this still feels like a bug or poor design at the very least. I would think that a Class called 'NumberValidator' would be able to take a Number as input. Is this actually a bug( if it is I'll file a bug report) or am I just missing something obvious here?

       

       

      PS. For anyone else who happens to run across this issue just use the mx version of NumberValidator. Since it's not based on GlobalizationValidatorBase it can handle 0s with no problem.

        • 1. Re: Spark NumberValidator Unable to Validate Zero?
          drkstr_1 Level 4

          This is a design flaw (bug) I would say. I started using explicit conditionals in my code for this very reason. It's more typing to do this, but I think it produces better code.

           

          IMHO, it would be better to say:

           

          if(value == null || value == "")

           

          (maybe even just isNaN(value) would be ok, but I think you would want to differentiate between an empty value and an invalid one)

           

          You may need to monkey patch (copy the file from the Flex source into your project using the same path, and make the modification there) to fix the issue.