I have a DataGrid which uses an itemRenderer to display the items in a DataGridColumn. The ItemRenderer is created in actionscript and contains the following code:
public class PromptingColumnRenderer extends Label {
public function PromptingColumnRenderer()
{
super();
}
//When the data is empty, display the correct prompt
override public function set data(newData:Object):void
{
super.data = newData;
var value:String=data.value as String;
var type:String=data.type as String;
if (value=="") {
this.setStyle("color","0x444444");
this.setStyle("fontStyle","italic");
if (type=="TEXT")
this.text="Enter Text";
else if (type=="NUMBER")
this.text="Enter a Number";
else
this.text="Choose a Date";
}
else {
this.setStyle("color","0x000000");
this.setStyle("fontStyle","normal");
this.text = value;
}
}
}
I set this as the ItemRenderer of my DataGridColumn with the following mxml:
<mx:DataGridColumn id="valueCol"
itemRenderer="com.x.x.x.PromptingColumnRenderer"
dataField="value" sortable="false" editable="true"/>
In this DataGrid column, I require number validation on only the data with the type=”NUMBER” field set. The problem is, when I try to declare a NumberValidator within my PromptingColumnRenderer actionscript code, the view recycles the renderer, and the NumberValidator attempts to validate non-numeric rows. What is the best way to validate only the rows with the type=”NUMBER” field set?
Don’t validate within your itemRenderer. Instead create a custom itemEditor. That is the piece that is created when he user enters into the field to enter new value. I believe it uses a textInput by default. But, you could use a numeric stepper, or a TextInput w/ a numericValidator.
Here is an article on itemRenderers.
I believe that is the best approach, but if you prefer to stick with what you have, and validate within the itemRenderer, then just add a conditional to prevent validation if the entered value is not a number.
Conceptually something like this: