Using validators in DataGrid - Flex
Asked Answered
S

2

8

i have an editable DataGrid, something like:

<mx:Datagrid editable="true" dataProvider="{arrayListPreferences}" id="preferencesGrid">
    <mx:columns>
        <mx:DataGridColumn header="col1" dataField="preference" editable="false"/>
        <mx:DataGridColumn header="col2" dataField="value" editable="true"/>
    </mx:columns>
</mx:Datagrid>

When the user edits the data there's a button that he clicks and calls a function that saves the data to a database, and in this function i have to validate the data before sending it. I want to use simple validators (NumberValidator, StringValidator, etc) but i don't know how to set the source of this validators to the specified rows in the second column.

Silvers answered 5/12, 2009 at 17:34 Comment(0)
S
8
<mx:NumberValidator source="{preferencesGrid.selectedItem}" property="value" 
    integerError="Enter Integer value"
    minValue="18" maxValue="50" domain="int" 
    trigger="{saveButton}" triggerEvent="click"
    valid="saveData();"/>

Set the property of validator to the dataField of the desired column.

Scurrilous answered 6/12, 2009 at 13:55 Comment(1)
Right, that works fine, but how can i set the source to a desired column instead of using {preferencesGrid.selectedItem}? ThanksSilvers
G
2
<mx:DataGridColumn editable="true" itemRenderer="MyTextInputItemRenderer"/>



public class MyTextInputItemRenderer extends TextInput{
        private var validator:StringValidator;
        public function MyTextInputItemRenderer(){
            validator = new StringValidator;
            validator.minLength=0;
            validator.property = "text";
            validator.source = this;
        }
        override public function set data(value:Object):void{
            super.data = value;
            validator.validate();
        }
    }
Genome answered 2/3, 2011 at 23:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.