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



5 Responses to Using Validators with Spark Cell Editors

  1. Don Kerr says:

    Thanks Darrell! Your post are really helping me with editable spark datagrid. Appreciate it!

    Don Kerr

  2. Don Mitchinson says:

    Love your articles and example code is great.
    I’m trying to use your NumberGridItemEditor but assign the maxvalue dynamically based on another cell’s value in the same row.
    It compiles with a warning ‘Data binding will not be able to detect assignments to “data”‘ but sems to work okay – but the maxvalue parameter doesn’t kick in.

    I was trying to use

    So if DayHrs == D then they can only enter 0 or 1, otherwise max is set to 12.

    Is there something else I need to do with the class?

    Thanks again

  3. Don Mitchinson says:

    It seems to have stripped my ‘if’ code out – maybe if I leave out the squiggly brackets?

    The code was an inline if:
    data.eqptDaysHrs == ‘D’ ? 1 : 12

  4. Don Mitchinson says:

    Was able to get it to work in ItemRenderer – but not as ItemEditor – thanks again for the example

  5. Siilk says:

    Thanks for the info! Very helpful.