Using Validators with Spark Cell Editors

At some point you will probably want to add a validator to a cell editor. This post shows how to use a validator with Spark cell editors. The Spark DataGrid contains two built-in cell editors, DefaultGridItemEditor and ComboBoxGridItemEditor, both of which extend GridItemEditor. Neither of these editors has a validator so you will want to create your own custom item editor to add validation. As an example I created an editor, NumberGridItemEditor, that uses the Spark number validator to ensure that the entered data is between 0 and 1000, inclusive.

To add a validator to a custom item editor is pretty simple. Just declare the validator and hook it up the input component.


<s:NumberValidator id="numberValidator" source="{textInput}" property="text"



Since NumberGridItemEditor subclasses GridItemEditor it picks up some built-in support for validators. When the editor’s data is being saved, the GridItemEditor first checks if the data is valid by calling the validate() method. If the data is not valid then the editor won’t try to save it. If the Enter key is used to end the editor session then the editor will remain displayed so the input may be corrected. But if the user clicks away from the editor then the editor session will be cancelled if the data is not valid and therefore cannot be saved. If the default behavior doesn’t work for you then it can be modified by overriding the validate() and save() methods of GridItemEditor.

Download the project