in a rush

experience is everything

in a rush header image 2

Flex Custom Validation

March 28th, 2006 · No Comments

In Flex, you use a validator to ensure the values in the fields of an object meet certain criteria. You can assign each field of an object to a single validator. I”ve used the mx.validators.Validator class, which is an ActionScript class that you can extend to add your own validation logic as I have below. I also added a <mx:Model> tag to hold the error message that will be displayed if the validation fails. The model can then be populated with error messages that are maintained outside of the Flex application.

The <mx:Validator> tag must always be immediate children of the root tag of an MXML file. The Validator tags have one required property, the field to validate. I also added validate and listener properties which allow me to specify the validation logic to execute and the validation listener.

First declare a validator in MXML using the <mx:Validator> tag. To validate data that is bound to a data model, you should declare the appropriate validator in the same file as the bindings you make to the data model.
NOTE: If the validator and bindings aren”t in the same file, the validator is not triggered when data is copied into the model; however, you could trigger validation in an ActionScript function. You do not have to declare the validator in the same file as the data model.
Second, add a call to the isValid() method specifying the field to validate in the event handler for the submit. In this example, it is the submit function that handles the click event of the <mx:Button>.

Finally, define the validation logic for the Validator’’s validate event. When the validation fails, call the validationError() method.

  1. <!– data model holding custom error messages –>
  2. <mx:Model id="modelData">
  3. <errorMessage>{ errorText.text }</errorMessage>
  4. </mx:Model>
  5.  
  6. <mx:Script>
  7. <![CDATA[
  8. /* Event handler for the button click.
  9. * It will call the Validator''s isValid() method.
  10. */
  11. public function submit():Void
  12. {
  13. mx.validators.Validator.isValid( this, "yearInput" );
  14. }
  15.  
  16. /* Event handler called to validate the specified field. */
  17. public function validateMe( eventObj:Object ):Void
  18. {
  19. if( eventObj.value.text == "" ) {
  20. eventObj.validator.validationError( "nullValue", modelData.errorMessage, null );
  21. }
  22. }
  23. ]]>
  24. </mx:Script>
  25.  
  26. <mx:Validator field="yearInput" validate="validateMe( event )" listener="yearInput" />
  27.  
  28. <mx:TextInput id="errorText"/>
  29. <mx:TextInput id="yearInput"/>
  30.  
  31. <mx:Button label="Submit" click="submit()"/>

For fun you might try removing the listener from the Validator to see the results.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati

Tags: Flex

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

Leave a Comment