3 Replies Latest reply on Nov 30, 2010 5:11 AM by Aurelien Vannieuwenhuyze

    Validator not working

    linrsvp Level 1

      This is how my code looks :

       

       

      <fx:Declarations>
              <mx:NumberValidator property="selectedIndex"
                                  minValue="0" lowerThanMinError="Please select a Project" id="numValidator"
                                  />

       

              <mx:StringValidator  id="strValidator" property="text" required="true" requiredFieldError="Please Enter A Value" />
              <mx:EmailValidator id="emailValidator" property="text" invalid="emailValidator_invalidHandler(event)" />
          </fx:Declarations>

       

      and I call the validators like this :

       

                      var e1:ValidationResultEvent = numValidator.validate(projectList);
                      var e2:ValidationResultEvent = strValidator.validate(ticketTitle);
                      var e3:ValidationResultEvent = strValidator.validate(ticketBody);
                      var e5:ValidationResultEvent = numValidator.validate(ticketPriority);

       

      But the validations arent working ... i.e, when i submit the form with the text input fields empty, I dont get any alerts. on the contrary, I get an error alert even after I select a value from the drop down list .. When i checked the function definition for the validate function it says smthing like this :

       

      Performs validation and optionally notifies the listeners of the result.

      Parameters:
      value Optional value to validate. If  null, then the validator uses the source and property properties to determine the value. If you specify this argument, you should also  set the listener property to specify the target component for any  validation error messages.

      What does it mean when it says, if I pass the "value" argument to this function (which I have).. I shld also set the "listener" property...

      I dint understand that.. is that why my validations arent working ?
        • 1. Re: Validator not working
          Aurelien Vannieuwenhuyze

          Hello,

           

          Have you notified the source of the validator ?

           

           

          Regards.

           

           

           

          <?xml version="1.0" encoding="utf-8"?>

          <s:Application

           

           

          xmlns:fx="http://ns.adobe.com/mxml/2009"

          xmlns:s="

          library://ns.adobe.com/flex/spark"

          xmlns:mx="

          library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

           

           

          <fx:Declarations>

           

           

          <mx:NumberValidator property="selectedIndex" minValue="0" lowerThanMinError="Please select a Project" id="numValidator" />

           

           

          <mx:StringValidator id="strValidator" property="text" required="true" requiredFieldError="Please Enter A Value" />

           

           

          <mx:EmailValidator id="emailValidator" property="text" invalid="emailValidator_invalidHandler(event)" />

           

           

          </fx:Declarations>

           

          <fx:Script>

          <![CDATA[

           

           

           

          import mx.events.ValidationResultEvent;

           

          private var vResult:ValidationResultEvent;

           

           

           

          public function emailValidator_invalidHandler(e:Event):void

          {

           

          }

           

           

           

          public function submit():void

          {

          strValidator.source = txt_mail;

          vResult = strValidator.validate();

           

            

          }

           

          ]]>

          </fx:Script>

           

           

           

           

           

          <mx:Form x="34" y="47" id="form_mail">

           

           

          <mx:FormItem label="Email : ">

           

           

          <s:TextInput id="txt_mail"/>

           

           

          </mx:FormItem>

           

          <mx:FormItem width="183" horizontalAlign="right">

           

          <s:Button label="OK" click="submit()"/>

           

          </mx:FormItem>

           

          </mx:Form>

           

           

           

           

          </s:Application>

          • 2. Re: Validator not working
            linrsvp Level 1

            oh boy thank u so much! ... but just so I know, what is the first argument "value:Object" of the validate function ? I tght its the id of the textinput or whatever form field we want to validate... apparently it isnt, so what exactly is it ?

            • 3. Re: Validator not working
              Aurelien Vannieuwenhuyze Level 1

              By default, Flex uses the source, property and listener values

              Source = component to validate

              Property = property to verify

              Listener = Source = Component to display error.

               

              With the validate method, we can specify the property (text,personal property of your custom component...), but we must indicate the listener to use.

               

              Like this :

               

              <mx:StringValidator

               

              id="strValidator" required="true" requiredFieldError="Please Enter A Value" />

               

               

               

              public  function submit():void

              {

               

              strValidator.listener = txt_mail;

              vResult = strValidator.validate(txt_mail.text);

               

              }

               

               

              1 person found this helpful