5. Data Validation
Client Propagation for Server Validation Errors
Tide integrates with Hibernate Validator, and possibly others, to propagate the server validation errors to the client UI components. The validator integration is based on the GraniteDS exception handling framework. A server exception converter is registered to handle the InvalidStateException, and a client exception handler can be registered with:
Seam.getInstance().addExceptionHandler(ValidatorExceptionHandler);
This exception handler dispatches a validation event on the context.
To complete the integration, a TideEntityValidator Flex component can be attached to any UI component:
<mx:Application
...
xmlns:tv="org.granite.tide.validators.*">
<tv:TideEntityValidator id="teval" entity="{tideContext.booking}"
property="creditCardName" listener="{creditCardNameInput}"/>
<mx:TextInput id="creditCardNameInput" text="{tideContext.booking.creditCardName}"/>
...
</mx:Application>
You can have more control on the validation behaviour by directly listening to validation events on the context:
public function setup():void { tideContext.addEventListener(TideValidatorEvent.INVALID, validationHandler); } private function validationHandler(event:TideValidatorEvent):void { var invalidValues:Array = event.invalidValues; for each (var iv:Object in invalidValues) { var invalidValue:InvalidValue = iv as InvalidValue; Alert.show( "Invalid property: " + invalidValue.path + " on object " + invalidValue.bean ); } }
Remote Validation of Input Fields
Another possibility is to have an input validator that calls the server when the validation is triggered.
With Seam, it then uses the Validators component to get a proper ClassValidator, and thus just works with Hibernate Validator for now. With other server technologies, it uses a built-in validators handler which still also works only with Hibernate Validator.
The use of this component is quite simple and very similar to any other Flex validator, with additional parameters to define the entity to validate.
<tv:TideInputValidator id="tival" source="{creditCardNameInput}" property="text" entity="{tideContext.booking}" entityProperty="creditCardName" entityManager="{tideContext}"/> <mx:TextInput id="creditCardNameInput" text="{tideContext.booking.creditCardName}"/>
The entityManager property of the validator may be optional if the entity is maintained in the context (i.e., ctx.myEntity) and has a proper entityManager defined (with meta_getEntityManager).
Integration with Seam control messages
When using Seam, it is possible to propagate server-side status messages to the corresponding Flex control with the TideControlValidator component.
<mx:Application
...
xmlns:tsv="org.granite.tide.seam.validators">
<mx:TextInput id="username"/>
<tsv:TideControlValidator source="{username}" property="text"/>
</mx:Application>
