Tips & Tricks – ItemEditors – III
In the earlier articles (I, II) , we saw how to use itemEditors on a flex datagrid and also how to validate data that is entered in the itemEditor. Lets take the example in the previous article and improve it a little further.Lets look at the doValidation() method again: We were comparing if the entered value is greater than the upperBound and throw an error message as an Alert in the example. Flex comes with some really cool built in validators that provide a much more usable user experience than throwing an Alert box to the user when an error has occured. In this example, lets try to incorporate a NumberValidator to do the same validation that we were doing earlier using the if conditional.
The code
Well, things havent changed too much from our earlier example, except for the following method – doValidation()
//Validate the data entered / stepped in the itemEditor
private function doValidation(event:DataGridEvent):void
{//Storing the selection made in the itemEditor to a variable
var selectedData:Object = CustomNumericStepper(datagrid.itemEditorInstance).value
//Store the upper bound to compare
var upperBound:Number = event.target.selectedItem.value;
//create a number validator instance. This can be extended to any validators
nv = new NumberValidator()
//specify the source – in this case the itemEditor instance
nv.source = CustomNumericStepper(datagrid.itemEditorInstance);
//Set the property to validate against. NOTE: always set this, or the validator will throw RTE
nv.property = “value”
//Set the maxvalue to the upperBound
nv.maxValue = upperBound;
//Call validate and take the return value. NOTE: validate() method sends two events valid / invalid based on the validation.
//val contains an event reference
var val:* = nv.validate(selectedData);
//check if validator returned invalid.
if(val.type == “invalid”)
{
//Prevent default action.
event.preventDefault();
}}
So, whats different here? We are creating a new instance of a NumberValidator (nv) here. Then the source property of the validator is assigned to the control, whose data we are interested in validating. In our case, its the itemEditorInstance – the custom numeric stepper. Important point to note here – once you have set the source property, always set the “property” property (this is NOT redundant typing, there is a property called property for a validator
) of the validator to the property that we are validating against. Set the maxValue of the validator to the upperBound and call validate(value:Object) method on the validator. The validate() method accepts the value that we are validating.
When you run the example, you can see that the validator puts a red box around the wrongly entered field and when you move the mouse over, you can see that the error message is displayed as a red tooltip. Isn’t that cool? Do look up the documentation for the validators, there are many more properties that can be used to customize the validation parameters, error messages etc.








June 4, 2007 - 12:25 pm
Link broken for “View Sample Application” as well as Download source.
June 28, 2007 - 7:32 pm
This is great stuff! Appreciate it.
June 28, 2007 - 7:35 pm
One of the most useful blog post series I’ve seen. nice.
June 29, 2007 - 11:18 pm
What if the user mouse clicks away to change focus vs hitting enter – the preventDefault doesn’t seem to do anything.
July 12, 2007 - 9:36 pm
Your source does not match up to your sample app. In your sample app you can type into this datagrid instead of using the stepper. however, in your source code this function does not work.
September 5, 2007 - 7:52 pm
Dude,
Why is it that when i implemented this code it works differently than the sample app? I like the idea of being able to trap the focus in the control until the error is fixed. It appears as though your sample app disables the other rows in the column until the validation is satisfied because when you click in another coulmn the control is destroyed and focus is changed.
Is there more code doing this functionality that I see in the sample app than what was presented in the source code? When I ran the code supplied all works but this aforementioned functionality.
January 9, 2008 - 4:56 am
what if I have two item editors in a same datagrid and want to validate both of them with a different logic.. I think itemEditEnd will fire a single function, but if I have to check these values on different conditions then probably there is a trap.. any solutions for this issue?
October 30, 2009 - 1:02 pm
This is great stuff! and very helpful..Thanks for providing such nice example