Joshua Fox
Joshua Fox

Reputation: 19655

Validate DataGridColumn cells individually

How can I validate the cells in a DataGridColumn individually? (ActionScript 3.5) The validation is configured per-cell, based on fields in the given row. For example

FIELD               VALUE   TYPE
age                 13      Integer
height              13x3    Integer
registered          true    Boolean
temperature         98.G6   Float

In this case, of course 13x3 and 98.G6 would be invalid.

It's easy to write a Validator ; and to access the data provider objects.

But how do I get individual access to the GUI cell objects so I can set the errorString on an individual cell, either directly or through a Validator?

The itemRenderer/ TextInput control is re-used across the cells for performance reasons, so accessing the GUI-level objects is tricky.


Edit

Answers:

  1. One way to validate and display the invalidation markings, but not per-cell, is to validate all data-provider objects and then set the errorString on the entire grid.

  2. One way to validate per-cell is on the itemEditEnd event handler. (See these pages A B C D). One disadvantage is that it only allows access to the cells from the "inside", not in an action that validates the grid on command.

  3. A custom itemRenderer is another possibility, as in the answer below, but like 3 above, it only allows access to the cells from the "inside", not in an action that validates the grid on command.

  4. See Richard Haven's answer below.

  5. And here's how to access the GUI objects: The list of relevant GUI objects is a protected field; so you can access it by subclassing, then iterate over the GUI-components which represent the cells and set the errorString on each one.

Upvotes: 2

Views: 3383

Answers (3)

Richard Haven
Richard Haven

Reputation: 1154

If you are looking for arbitrary validation (e.g. on a button or page navigation) rather than immediate navigation (e.g. on cell exit or end-of-edit), then the data is in the underlying dataProvider. I would do validations there rather than dig around inside the grid.

You can add a flag to the data item so the item renderer displays it as an error (or use an external list to flag it).

Cheers

Upvotes: 1

matthewwithanm
matthewwithanm

Reputation: 4142

Are you sure you actually want to access the individual cells' DisplayObjects? The component manages instances so that it only creates as many as it needs to display (so that huge datasets don't require a huge number of DisplayObjects on screen).

I think a better alternative would be to provide your DataGridColumn with a custom itemRenderer. You can write this class to accept a validator and update its appearance, and there are a bunch of great tutorials around about that.

Upvotes: 1

Kyra
Kyra

Reputation: 5407

This website at BigResource asks how to access an individual cell. The third post answers there question and provides a link to a better resource than this. Figured you would want both. Hopefully this helps.

Upvotes: 2

Related Questions