//But this gives you an idea if you needed to do it conditionally based on the situation.
if(dataField!='lastname')
{
Alert.show('Bro, you can only edit the last name.');
event.preventDefault();
}
else
{
if(country!='USA')
{
Alert.show('You can only edit if the country is USA');
event.preventDefault();
}
}
}
}
private function checkIt(myEvent:Event):void
{
dg.invalidateList();
}
//This gets fired when the itemEditor is about to be destroyed. As a result of hitting enter, tab, or mouse clicking away.
private function onEditEnd(event:DataGridEvent):void
{
var dataField:String = event.dataField;
var fCell:Array=[event.columnIndex,event.rowIndex];
//This tells us what the new data is about to be before it's committed.
var newData:String = TextInput(event.currentTarget.itemEditorInstance).text;
nv = new StringValidator();
//This if statement isn't nescessary in this example. But say you needed to validate
//different cells differently... here's where you'd check which cell we're dealing with.
if(dataField == 'lastname')
{
//specify the source - in this case the itemEditor instance
//We only care to do this if they started typing in something
nv.source = event.currentTarget.itemEditorInstance;
nv.property = "text";
nv.minLength = 3;
nv.requiredFieldError = "at least 3 chars required";
nv.tooShortError = "at least 3 chars required";
var val:* = nv.validate();
if(val.type == "invalid")
{
event.preventDefault();
if(newData.length!=0)
callLater(maintainEdit,fCell); //if they typed in something and it didn't validate
} //we want to get Flex to re-invoke the editor on this cell.
else //**NOTE: Once this function onEditEnd completes, the validator won't highlight
{ //the cell anymore, because the itemEditor gets destroyed...
callLater(maintainFocus);
}
}
else
callLater(maintainFocus);
}
//this checks to see if we're still in edit mode, and if we're not..invoke it
private function maintainEdit(colIndex:int,rowIndex:int):void
{
var editCell:Object = {columnIndex:colIndex, rowIndex: rowIndex};
if(dg.editedItemPosition==null)
{
//This will invoke the datagrid's itemEditBegin
dg.editedItemPosition = editCell;
//Because the itemEditor got destroyed, in the above line we're essentially creating a new itemEditor
//So we want to invoke the validator right away so we can highlight to the user that we're not happy
//With the validation...
callLater(validateCurrentEditor);
}
}
private function validateCurrentEditor():void
{
if(dg.itemEditorInstance!=null)
{
nv.source = dg.itemEditorInstance;
nv.validate();
}
}
//prevents enter/tab from moving onto the next cell after finishing editing.
private function maintainFocus():void
{
dg.editedItemPosition = null;
}
]]>