Packageorg.granite.validation
Classpublic class FormValidator
InheritanceFormValidator Inheritance mx.validators.Validator

The FormValidator class is Flex utility that simplifies the validation framework usage with input components. It performs validation on the fly whenever the user enters data into graphical inputs and automatically displays error messages when these data are incorrect, based on constraint annotations placed on bean properties.


Example
The following code excerpt illustrates a typical usage (Flex 4 with bidirectional bindings):
  
  [Bindable]
  public class Person {
  
      [Size(max="10")]
      public var firstname:String;
  
      [NotNull] [Size(min="2", max="10")]
      public var lastname:String;
  }
  
<fx:Declarations> <v:FormValidator id="fValidator" form="{personForm}" entity="{person}"/> </fx:Declarations> <fx:Script> [Bindable] protected var person:Person = new Person(); protected function savePerson():void { if (fValidator.validateEntity()) { // actually save the validated person entity... } } protected function resetPerson():void { person = new Person(); } </fx:Script> <mx:Form id="personForm"> <mx:FormItem label="Firstname" required="true"> <s:TextInput text="@{person.firstname}"/> </mx:FormItem> <mx:FormItem label="Lastname" required="true"> <s:TextInput text="@{person.lastname}"/> </mx:FormItem> </mx:Form> <s:Button label="Save" click="savePerson()"/> <s:Button label="Cancel" click="resetPerson()"/>



Public Properties
 PropertyDefined by
  entity : Object
The entity, with constraint annotations, to be validated.
FormValidator
  entityPath : String = "null"
The path of the entity to be used when the entity which will be validated is not the root of the bindings.
FormValidator
  form : Object
The form component that contains inputs bound to the entity properties (may be a Form or any other Container subclass).
FormValidator
  groups : Array = null
The validation groups to be used, as an array of Class names.
FormValidator
  prioritaryComponentProperties : Array
[static] Array of property names that will be considered prioritarity as candidates for UI input bindings.
FormValidator
  unhandledViolationsMessage : String
[read-only] A message containing all unhandled violations messages, separated by new lines.
FormValidator
  validateOnChange : Boolean = true
Should validation be done on the fly? Otherwise, validation will be only done when an input loses focus.
FormValidator
  validatorFactory : ValidatorFactory
The ValidatorFactory to be used in the validation process (initialized with the the default instance).
FormValidator
Protected Properties
 PropertyDefined by
  actualListeners : Array
[read-only]
FormValidator
  _id : String
FormValidator
Public Methods
 MethodDefined by
  
Returns the unhandled violations of the last global validation as an array of ConstraintViolations.
FormValidator
  
getViolations():Array
Returns the result of the last global validation as an array of ConstraintViolations.
FormValidator
  
validateEntity():Boolean
Validate the underlying entity instance for the currently set groups.
FormValidator
Protected Methods
 MethodDefined by
  
doValidation(value:Object):Array
FormValidator
  
handleResults(errorResults:Array):ValidationResultEvent
FormValidator
Events
 EventSummaryDefined by
   Dispatched when validation is done, with a (possibly empty) array of ValidationResult for any ConstraintViolation that couldn't be associated with any input.FormValidator
Public Constants
 ConstantDefined by
  UNHANDLED_VIOLATIONS : String = "unhandledViolations"
[static]
FormValidator
Property detail
actualListenersproperty
actualListeners:Array  [read-only]

Implementation
    protected function get actualListeners():Array
entityproperty 
entity:Object  [read-write]

The entity, with constraint annotations, to be validated. This entity must implement the IEventDispatcher interface, explicitly or not (ie: annotated with [Bindable]).

This property can be used as the source for data binding.

Implementation
    public function get entity():Object
    public function set entity(value:Object):void
entityPathproperty 
public var entityPath:String = "null"

The path of the entity to be used when the entity which will be validated is not the root of the bindings.

formproperty 
form:Object  [read-write]

The form component that contains inputs bound to the entity properties (may be a Form or any other Container subclass).

This property can be used as the source for data binding.

Implementation
    public function get form():Object
    public function set form(value:Object):void
groupsproperty 
public var groups:Array = null

The validation groups to be used, as an array of Class names. Default is null, meaning that the Default group will be used.

_idproperty 
protected var _id:String
prioritaryComponentPropertiesproperty 
public static var prioritaryComponentProperties:Array

Array of property names that will be considered prioritarity as candidates for UI input bindings. Can be used to disambiguate some cases where the component cannot reliably determine which property holds the binding. Values can be added/removed by the user is necessary.

unhandledViolationsMessageproperty 
unhandledViolationsMessage:String  [read-only]

A message containing all unhandled violations messages, separated by new lines.

This property can be used as the source for data binding.

Implementation
    public function get unhandledViolationsMessage():String
validateOnChangeproperty 
public var validateOnChange:Boolean = true

Should validation be done on the fly? Otherwise, validation will be only done when an input loses focus. Default is true.

validatorFactoryproperty 
public var validatorFactory:ValidatorFactory

The ValidatorFactory to be used in the validation process (initialized with the the default instance).

Method detail
doValidation()method
protected override function doValidation(value:Object):Array

Parameters
value:Object

Returns
Array
getUnhandledViolations()method 
public function getUnhandledViolations():Array

Returns the unhandled violations of the last global validation as an array of ConstraintViolations. Unhandled violations are violations that couldn't be associated to any input during the last global validation (thus, they couldn't be displayed anywhere).

Returns
Array — the unhandled violations of the last global validation as an array of ConstraintViolations.
getViolations()method 
public function getViolations():Array

Returns the result of the last global validation as an array of ConstraintViolations.

Returns
Array — the result of the last global validation as an array of ConstraintViolations.
handleResults()method 
protected override function handleResults(errorResults:Array):ValidationResultEvent

Parameters
errorResults:Array

Returns
ValidationResultEvent
validateEntity()method 
public function validateEntity():Boolean

Validate the underlying entity instance for the currently set groups.

Returns
Booleantrue if validation succeeds, false otherwise.
Event detail
unhandledViolationsevent 
Event object type: mx.events.ValidationResultEvent

Dispatched when validation is done, with a (possibly empty) array of ValidationResult for any ConstraintViolation that couldn't be associated with any input.

See also

mx.events.ValidationResultEvent
mx.events.ValidationResult
Constant detail
UNHANDLED_VIOLATIONSconstant
public static const UNHANDLED_VIOLATIONS:String = "unhandledViolations"