Skip navigation
yusufnazir
Currently Being Moderated

Validator not returning the results.

Mar 29, 2012 4:54 AM

Hi All,

 

I have extended the StringValidator class in order the create a username validator. When a new user is being registered, the username is validated on the server to check if it exists.

The code is not throwing errors and i know the remoteobject calls work. The push sets the message, but the problem is that the result is not returned.

 

Is the flow correct?

 

public class PosUsernameValidator extends StringValidator {
          
          private var userServiceRO:RemoteObject = new RemoteObject();
          private var validatorResults:Array = new Array();
          
          public function PosUsernameValidator(){
               super();
               userServiceRO.destination = UserService;
               userServiceRO.getUserByUserName.addEventListener(result, getUserByUserNameRH);
               userServiceRO.getUserByUserName.addEventListener(fault, getUserByUserNameFH)
          }
          
          override protected function doValidation(value:Object):Array {
               validatorResults = super.doValidation(value); 
               return doForUserName(value);
          }
          
          private function doForUserName(value:Object):Array{
               userServiceRO.getUserByUserName(String(value));
               return validatorResults;
          }
          
          private function getUserByUserNameRH(r:ResultEvent, t:Object = null):void{
               var user:User = r.result as User;
               if(user!=null){
                    validatorResults.push(new ValidationResult(true, null, Username error,Username already taken!));
               }
          }
          
          private function getUserByUserNameFH(f:FaultEvent, t:Object = null):void{
               Alert.show(ObjectUtil.toString(f));
          }
          
     }
 
Replies
  • Currently Being Moderated
    Mar 29, 2012 7:10 AM   in reply to yusufnazir

    At first glance I would say that trying to combine your client-side and server-side validation into a single class like that is not a good idea. In doForUserName() you are making an asyncronous server call. The validatorResults will get returned immediately, so by the time your handler method executes it is already too late. Rather than trying to do both sets of validation at once, remove the server-side code from this class and use it as a normal flex validator. Then in your component (or better yet in your controller class if you follow an MVC pattern) call your service method after the client side validator returns a valid result.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 29, 2012 10:08 AM   in reply to yusufnazir

    Once you have the message string from the server-side error, bind it to the 'errorString' property of whichever input component you want to display the error on. Doing that will give you the red border and tooltip just like if you used a validator. Just remember to set the error string back to "" to clear the message once the validation passes.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points