views:

178

answers:

3

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.

+2  A: 

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.

Kyra
Thank you. The website you link seems to show how to access the *data* in a given cell (what was the "third post" you refer to?), whereas I am looking for a way to iterate over the *GUI-components* which represent the cells. See my edit to the question, above, which gives some links. It seems that Flex destroys the TextInput or other editor control each time you exit the cell.I would like to validate when an OK button is pressed. Invalid cells will get the red border and error tooltip usual for validation. I should note that it is possible to get the CellRenderer at the given coordinates.
Joshua Fox
I should note that it is possible in fl.controls.DataGrid, but not in mx.Controls.DataGrid to get the CellRenderer at the given row and column.
Joshua Fox
+1  A: 

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.

matthew
Thanks -- but I want to validate when a button is pressed on the "outside" of the DataGrid, as in the MXML sample below. I do know how to validate from "inside" the DataGridEvent handler, optionally using a custom itemRenderer, but I need to block the closing of the dialog which includes this code if validation fails.
Joshua Fox
+1  A: 

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

Richard Haven
Of course we do validation against the data in the *dataProvider*. The question is how to set the GUI elements, e.g. the *errorString*. Your idea of the validation flag in the item renderer is good, but still does not answer how to validate the whole grid from, e.g., a "validate" button outside the grid. This is important when you have a dialog that is to be closed but must be validated on closing (blocking the closing as needed).
Joshua Fox