5 Replies Latest reply on Jan 28, 2011 9:03 AM by _spoboyle

    clearing validation errors

    _spoboyle Level 4

      I have a form contianing 2 textinputs and a submit button

      one of these has 2 validators attached to it

      • a string validator (which is auto triggered)
      • and a custom password validator (triggered on submit)

       

      if you enter a password that passes validation on the string validator but is not a correct password (e.g. "passwor")

      you receive the error message "incorrect password"

      if you then edit the password textinput the error clears (ive used the textinput's change event to set the errorString to "")

      Now if you edit the password textinput to fail the string validator (e.g. "pass") and move focus away from the password textinput (e.g. by pressing tab)

      the error tooltip now displays (correctly) the string validator's error message but also (incorrectly) it displays the previously cleared custom password's error message.

      Surely this shouldn't be generated again until submit is clicked again?

      any help would be appreciated

       

      required username = "username"

      required password = "password"

       

      Main.mxml

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     xmlns:mx="library://ns.adobe.com/flex/mx"
                     creationComplete="initApp()" xmlns:local="*">
         
          <fx:Script>
              <![CDATA[
                  import mx.events.ValidationResultEvent;
                  private function initApp():void
                  {
                  }
                 
                  private function submit():Boolean
                  {
                      var event:ValidationResultEvent = passwordValidator.validate();
                      if (event.type == ValidationResultEvent.VALID)
                      {
                          return true;
                      }
                      return false;
                  }
              ]]>
          </fx:Script>   
         
          <fx:Declarations>
              <mx:StringValidator id="passStringValidator"
                                  minLength="6"
                                  source="{password}"
                                  property="text"/>
             
              <local:PasswordValidator id="passwordValidator"
                                       source="{password}"
                                       property="text"
                                       usernameSource="{username}"
                                       usernameProperty="text"
                                       triggerEvent=""/>
          </fx:Declarations>
                 
          <s:VGroup>
              <s:TextInput id="username"  text="username"/>
              <s:TextInput id="password" change="{password.errorString = ''}"/>
              <s:Button id="submitButton" label="submit" click="submit()"/>
          </s:VGroup>
                 
      </s:Application>

       

      PasswordValidator.as

      package
      {   
          import mx.validators.ValidationResult;
          import mx.validators.Validator;
         
          public class PasswordValidator extends Validator
          {
              public var usernameSource:Object;
              public var usernameProperty:String;
             
             
              public function PasswordValidator()
              {
                  super();
              }
             
              override protected function doValidation(value:Object):Array
              {
                  var results:Array = super.doValidation(value.password);
                 
                  // Return if there are errors
                  // or if the required property is set to false and length is 0.
                  var val:String = value ? String(value) : "";
                  if (results.length > 0 || ((val.length == 0) && !required))
                      return results;
                  else
                      return PasswordValidator.validatePassword(this, value, null);
              }
             
              private static function validatePassword(validator:PasswordValidator,
                                                       value:Object,
                                                       baseField:String = null):Array
              {
                  var password:String = value.password;
                  var username:String =  value.username;
                  if (username != null)
                  {
                      if (username != "username" || password != "password")
                      {
                          return [new ValidationResult(true, null, "FAIL_CODE", "Password is incorrect.")];
                      }
                  }
                 
                  return [];
              }
             
              /**
               *  @private
               *  Grabs the data for the confirmation password from its different sources
               *  if its there and bundles it to be processed by the doValidation routine.
               */
              override protected function getValueFromSource():Object
              {
                  var value:Object = {};
                 
                  value.password = super.getValueFromSource();
                 
                  if (usernameSource && usernameProperty)
                  {
                      value.username = usernameSource[usernameProperty];
                  }
                 
                  return  value;
              }
          }
      }