Validating a data model using the validate() method : Model « Data Model « Flex






Validating a data model using the validate() method

Validating a data model using the validate() method
          
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script>
        
            import mx.controls.Alert;
            import mx.events.ValidationResultEvent;
            import mx.validators.PhoneNumberValidator;
            
            private var phoneV:PhoneNumberValidator;
            private var phoneVResult:ValidationResultEvent;
            private var nameVResult:ValidationResultEvent;

            private function doValidate():void
            {
                phoneV = new PhoneNumberValidator();
                phoneV.source = myModel;
                phoneV.property = "phone";
                phoneV.listener=phoneTxt;
                phoneV.required = true;
                phoneV.allowedFormatChars = "+,-";
                phoneV.invalidCharError = "Insert valid chars";
                phoneV.wrongLengthError = "The number is too short or too long";
                phoneV.requiredFieldError = "This input is required";
                phoneVResult = phoneV.validate();
                
                if(phoneVResult.type==ValidationResultEvent.VALID)  
                {
                    Alert.show("Validated!", "Validation Message", 4);
                        
                }  else if(phoneVResult.type==ValidationResultEvent.INVALID) 
                {
                    Alert.show("NOT validated !", "Validation Message", 4);
                }
            }
          
    </mx:Script>
    <mx:Model id="myModel">
         <root>
         <name>{nameTxt.text}</name>
         <surname>{surnameTxt.text}</surname>
         <phone>{phoneTxt.text}</phone>
         </root>
    </mx:Model>
    <mx:Form id="myForm">
        <mx:HBox>           
            <mx:TextInput id="nameTxt"  />
            <mx:Label id="myLbl" text="" visible="false" />
        </mx:HBox>
        <mx:FormItem label="Insert Surname">
            <mx:TextInput id="surnameTxt" />
        </mx:FormItem>
        <mx:FormItem label="Insert Phone Number">
            <mx:TextInput id="phoneTxt" />
        </mx:FormItem>
        
    </mx:Form>
    
    <mx:Button label="Validate" click="doValidate();" x="284" y="172"/>
    
        
</mx:Application>

   
    
    
    
    
    
    
    
    
    
  








Related examples in the same category

1.Model With ValidatorModel With Validator
2.Use dot-delimited Strings with a nesting structure of elementsUse dot-delimited Strings with a nesting structure of elements
3.Define a Flex data model that contains two valuesDefine a Flex data model that contains two values
4.Combine binded data from Data ModelCombine binded data from Data Model
5.Validate Model DataValidate Model Data
6.Script-based models
7.Fill in data for Model
8.Define and reference ModelDefine and reference Model
9.Referencing repeated data in ModelReferencing repeated data in Model
10.Bind model to TextBind model to Text
11.Bind whole expression from Model to a Text controlBind whole expression from Model to a Text control
12.Build Expression from data bind in ModelBuild Expression from data bind in Model
13.Do calculation with binded Model dataDo calculation with binded Model data
14.The tag
15.An child tag with no value is considered null.
16.Use validate() method to validate a Model objectUse validate() method to validate a Model object
17.Get data from Model
18.Loop through a ModelLoop through a Model
19.Bind control's data to Model and use model data in another controlBind control's data to Model and use model data in another control
20.Converting an MXML data model in an ActionScript objectConverting an MXML data model in an ActionScript object
21.Model and bindingModel and binding
22.A Flex data model that contains two values.A Flex data model that contains two values.
23.TextModel ExampleTextModel Example
24.An employee model declared in an tag:
25.Model Tag Empty String
26.Bind field in Model to a Text controlBind field in Model to a Text control