Harish Sivaramakrishnan

Platform Evangelist @ Adobe

Tips & Tricks – ItemEditors – III

9 comments

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.

View Sample Application | Download Source

Written by vodka

June 4th, 2007 at 9:16 am

Posted in flex,tips & tricks

9 Responses to 'Tips & Tricks – ItemEditors – III'

Subscribe to comments with RSS or TrackBack to 'Tips & Tricks – ItemEditors – III'.

  1. Link broken for “View Sample Application” as well as Download source.

    nisheet

    4 Jun 07 at 12:25 pm

  2. This is great stuff! Appreciate it.

    Darth Sidious

    28 Jun 07 at 7:32 pm

  3. One of the most useful blog post series I’ve seen. nice.

    Tariq Ahmed

    28 Jun 07 at 7:35 pm

  4. What if the user mouse clicks away to change focus vs hitting enter – the preventDefault doesn’t seem to do anything.

    Darth Sidious

    29 Jun 07 at 11:18 pm

  5. 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.

    Doug

    12 Jul 07 at 9:36 pm

  6. 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.

    jmw

    5 Sep 07 at 7:52 pm

  7. 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?

    Rahul

    9 Jan 08 at 4:56 am

  8. [...] Comment! Read this post at my new blog – http://blog.flexgeek.in/2007/06/tips-tricks-itemeditors-iii/ [...]

  9. This is great stuff! and very helpful..Thanks for providing such nice example

    Pravin Mahida

    30 Oct 09 at 1:02 pm

Leave a Reply