6 Replies Latest reply on Mar 2, 2008 10:34 PM by VarioPegged

    Customizing validators

      I am using a numberValidator to validate a formfield.The field is NOT mandatory, but needs validation. So, I want to use the numberValidator to validate but I don't want the RED highliting of the textinput box or the red alert string that shows up.

      As far I understand , to achieve this, I have to write my own custom validator class extending the numberValidator and implement the doValidation method.

      1- Is this the only way ?
      2- If so , what should be implemented in the doValidation() method to disable this red highliting and the red alert string just for the specific 'this field is required' use case,since that field is not required, but rest of the validation needs to be done the way numberValidator does it.

      Any suggestions appreciated.Thanks.
        • 1. Customizing validators
          VarioPegged Level 2
          A little while back I needed very specific number validation and tooltip behavior. I wound up rewriting a major portion of the NumberValidator class and I created a custom tooltip class.

          For your case, I would say that if you don't need different number validation than what the NumberValidator class provides, then simply set the ToolTipManager.enabled = false. Of course, if you have multiple fields in the same view, this prohibits other fields from using tooltips, but you could enable it on a per field basis in a custom tooltip class.

          My custom tooltip class disabled the ToolTipManager, then I used ToolTipManager.createToolTip and ToolTipManager.destroyToolTip under very specific circumstances to display the tooltips for specific input fields using event.currentTarget of the ValidationResultEvent.

          My functionality always called for the focus border to be drawn based on the errorString, so I'm not quite sure how you want to handle that. Look at the drawFocus() method of UIComponent class to see if that'll disable it for you.

          Hope this helps.

          • 2. Re: Customizing validators
            bisonj9 Level 1
            I will have to try what you suggested. That might work to an extent . The field I want to validate needs to be an integer.So I would need that validation and tooltip default behaviour for that ,BUT the field is not mandatory.So I don't want the tooltip and the validation default behaviour[red box] that exists for a mandatory field.

            There are a couple of fields and only one of them needs to be entered.No specific field is mandatory.So the behaviour associated with 'mandatory field' should be disabled ,tooltip,redbox. If nothing is entered in this field, it should still be 'valid' and not invalid.
            • 3. Re: Customizing validators
              VarioPegged Level 2
              Are you validating the field on a submit button click? My validation was actually "live" or on the change event of the TextInput component as the user typed characters. You could set the validator's "required" property to false and still get the basic validation. Have you tried:
              myValidator.required = false;

              • 4. Re: Customizing validators
                bisonj9 Level 1
                If myValidator.required = false; then the validator wouldn't validate at all. I just want it not to validate when nothing is entered , i.e its not a required field. but when something is entered it should.

                My validation is live too, on the 'KeyUp; event.

                I tried removing val.length ==0 check in the following ,since that is what checks for the length of the object, but doesnt seem to work. Do we have to do anything other than just save when we change any framework classes because I made this change in the NumberValidator.as .

                override protected function doValidation(value:Object):Array
                var results:Array = super.doValidation(value);

                // Return if there are errors
                // or if the required property is set to <code>false</code> and length is 0.
                var val:String = value ? String(value) : "";
                if (results.length > 0 || ((val.length == 0) && !required))
                return results;
                return NumberValidator.validateNumber(this, value, null);
                • 5. Re: Customizing validators
                  bisonj9 Level 1
                  Ok here is what I want to know.I think by simply disabling the super.doValidation(value); in numberValidator.as , I might get what I want since the doValidation method of the validator base class simply checks if data is entered or not.

                  My question is if I change a framework class what exactly should I do to see that change reflected .I simply changed and saved and it doesn't seem to work. Not sure about monkey patching!

                  • 6. Customizing validators
                    VarioPegged Level 2
                    I never tried making a change to NumberValidator and saving it, so I don't have an answer there. What I did was to make the changes to the code in NumberValidator since they were so extensive, renamed it to something else and placed it in its own package within my app package structure, leaving original copyright info intact. You could probably do the same, make your changes to doValidation(), take out the line include "../core/Version.as" elsewhere in the code and change the package name within the class to the package you placed it in.

                    Then refer to it in your custom namespace in mxml:
                    <cnv:CustomNumberValidator > (or something similar)

                    or AS:
                    var myNumValidator:CustomNumberValidator = new CustomNumberValidator()