13 Replies Latest reply on Jul 17, 2009 6:03 AM by Gus187

    Newbie - how to use a validator

    Jerry62712

      I would like to use the NumberValidator, but I don't know how.  I have instantiated it and set several of the properties:

       

        var numVal:NumberValidator = new NumberValidator();
        numVal.allowNegative = false;
        numVal.maxValue = "99999";
        numVal.minValue = "0";
        numVal.source = value;
        trace(???);
      
      

      I think I have set min, max and no negatives.  Further, I think I have set the input number (in a variable called value) which comes in as a text string.  What I want to do is find the results as shown by question marks.

        • 1. Re: Newbie - how to use a validator
          _Natasha_ Level 4

          Hi,

          in docs there is an example http://livedocs.adobe.com/flex/3/langref/mx/validators/NumberValidator.html#includeExample sSummary

           

          To check string you need to call static method NumberValidator.validateNumber(...)

          • 2. Re: Newbie - how to use a validator
            Jerry62712 Level 1

            Sadly there is no example on that page of how to use a validator from a script.  I've actually hit it several times as I have gone through four pages of results from a google search and a couple went back there.  What they do have is an example of how to setup a mxml tag, but nothing on how to reach it via ActionScript.

             

            It could be that it simple isn't possible to invoke a validator from ActionScript and I've just wasted a day and a half trying to find out how.  I must have read 45-50 web pages from that google search and not a single one of them had a single example of doing that.

            • 3. Re: Newbie - how to use a validator
              Gus187

              Hi,

               

              the source property is the component that you want to validate (i.e some TextInput).

               

              after you set that properties you need to call the validate method whih returns a ValidationResultEvent which has a property type that can be either ValidationResultEvent.VALID or ValidationResultEvent.INVALID.

               

              HTH

              Gus

              • 4. Re: Newbie - how to use a validator
                sri harsha namala Level 2

                Hi,

                           You can set the NumberValidator using ActionScript Also ,

                            Create a NumberValidator object and assign the source to that Object.

                 

                          Go through the Code once i think this might help you.

                 

                <?xml version="1.0" encoding="utf-8"?>
                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()">
                    <mx:Script>
                        <![CDATA[
                            import mx.validators.NumberValidator;
                            private function init():void
                            {
                                var numVal:NumberValidator=new NumberValidator();
                                 numVal.allowNegative = false;
                                 numVal.maxValue = "99999";
                                 numVal.minValue = "0";
                                 numVal.property="text";
                                 numVal.source=numberText;
                            }
                        ]]>
                    </mx:Script>
                    <mx:TextInput id="numberText" />
                    <mx:TextInput id="StringText" />
                </mx:Application>

                • 5. Re: Newbie - how to use a validator
                  Jerry62712 Level 1

                  [Sigh.  I see we are back to mostly sub-hour response time]

                   

                  Can the source input data to the numberValidator be a variable and not a tag property?  I did find how to get the results out of the class, but it won't help if numberValidator is not able to validate numbers that are in a variable.  In an extensive google search I could not find a single example of using numberValidator in a script where the input was a variable.

                  • 6. Re: Newbie - how to use a validator
                    Jerry62712 Level 1

                    [Yea - it seems the hour it takes to get this reply box only applies to the first time - this box came up in just seconds]

                     

                    Do you know of any way to get numberValidator to validate a number that is in an ActionScript variable?

                    • 7. Re: Newbie - how to use a validator
                      Gus187 Level 1

                      Hi!

                       

                      I test it and it works with variables, but you'll have to use an attribute of the var... this is how I did it...

                       

                      <?xml version="1.0" encoding="utf-8"?>
                      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
                          <mx:Script>
                              <![CDATA[
                                  import mx.validators.ValidationResult;
                                  import mx.events.ValidationResultEvent;
                                  import mx.validators.NumberValidator;
                                 
                                  private var source:Object = {data:1};
                                  private function validateVar():void
                                  {
                                      source.data = data.text;
                                      var validator:NumberValidator = new NumberValidator();
                                      validator.source = source;
                                      validator.property = "data";
                                      validator.minValue = 10;
                                      var result:ValidationResultEvent = validator.validate();
                                      trace(result.type);
                                  }
                              ]]>
                          </mx:Script>
                          <mx:TextInput id="data" />
                          <mx:Button label="validate var" click="validateVar();" />   
                      </mx:Application>

                       

                      This is just a quick test... you shouldn't use an object, instead create a VO. I don't know if this is the best way to use validators...

                       

                      HTH

                      Gus

                      • 8. Re: Newbie - how to use a validator
                        Jerry62712 Level 1

                        As a newbie, I want to make sure I understand the answer.

                         

                        It seems you are creating a MX TestInput tag called "data".  You also create an ActionScript variable of type object that is bound to the MX tag.  Then you assign that variable to the validator and let it do its thing.

                         

                        Will the MX tag be visible on the form?  I have MX tags already visible for each of the possible input fields.  But no form is displayed when this is called programatically.  When this is invoked via the web the controls validate the input (range, non-negative, numeric).  If I assign values to the existing MX tags, will that raise an error if the input is bad?  I would assume it would.  But then I wouldn't need the validator at all.

                         

                        But back to understanding the code - and I appreciate the time you spend putting it together!

                         

                        The data would have to be in the MX tag and then assigned to the variable.  But what does the binding mean?  Why the ":1"?  And if the data were already in an MX tag, why not just use that in the validator?  In my case the data is coming into the program from a query string on the URL and not from any MX tag.

                        • 9. Re: Newbie - how to use a validator
                          Gus187 Level 1

                          maybe it wasn't the best example ever ... the MX:TextInput I used only to be able to change the data that I wanted to validate in run time, if you remove the first line in the validateVar method (source.data = data.text;) and the MX:TextInput it will work and will perform the validation with 1 (data:1).

                           

                          The MX tag of the text input will be visible in the form, but as I said above its not necessary. I don't know if its part of you question but with private var source:Object = {data:1}; I'm defining an object with an attribute called data with a value of 1, which later I use in the validation.

                           

                          I'll post you a new example so it'll be more clear...

                           

                          <?xml version="1.0" encoding="utf-8"?>
                          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
                              <mx:Script>
                                  <![CDATA[
                                      import mx.validators.ValidationResult;
                                      import mx.events.ValidationResultEvent;
                                      import mx.validators.NumberValidator;
                                     
                                      private var source:Object = {data:1};
                                      private function validateVar():void
                                      {
                                          var validator:NumberValidator = new NumberValidator();
                                          validator.source = source;
                                          validator.property = "data";
                                          validator.minValue = 10;
                                          var result:ValidationResultEvent = validator.validate();
                                          trace(result.type);
                                      }
                                  ]]>
                              </mx:Script>
                              <mx:Button label="validate var" click="validateVar();" />   
                          </mx:Application>

                           

                          Here you have no MX tag, and the validation will be performed with 1 (data:1).

                          If I assign values to the existing MX tags, will that raise an error if the input is bad?  I would assume it would.  But then I wouldn't need the validator at all.

                          I don't know if I get this right, but if you assign the service response to the MX tags, and each MX tag has a validator it will not raise an error (exception) but it will be validated and the MX:textInput will be "invalid" (with a red square surrounding it)

                           

                          If I wasn't clear with some thing let me know...

                          • 10. Re: Newbie - how to use a validator
                            Jerry62712 Level 1

                            Bingo!

                             

                            You, sir, are a genius.

                             

                            The key is you can't assign a variable string to the numberValidator.  Once I assigned the string to an object with an attribute of "data" and set the validator property to "data" and set the source to the object, then it functioned as expected.  A very long trip, but with a most happy conclusion.

                             

                            As a newbie, I have nothing but questions, but at least I can move on to the next one.

                            • 11. Re: Newbie - how to use a validator
                              Gus187 Level 1

                                  I'm glad you've got you thing moving... and love you attitude , but I'm still thinking about your approach... let me see if I understood it complete it...


                              You want to validate the response of the server before assign it to the fields in the view?? if this is the case, have you thought to assign the response directly to the fields, which will have the validators. this way when you receive invalid data each invalid field will have it's border red (invalid).

                              • 12. Re: Newbie - how to use a validator
                                Jerry62712 Level 1

                                The form will never display in these cases.  Nor will the web page for that matter.  The application is being invoked from a program and the results (once I figure out how to do this) will be in the return stream to that program.  What happens is you call a number (1-800-843-6154) and pick option #5.  The system will prompt you to enter some stuff on the phone keypad and then it will execute my application to find out what your potential benefits for for food stamps will be.  My application has to return an output stream in XML format that their program will read.  It then says the answers to the guy calling into the system.

                                 

                                The system is also available via the web for those that won't use the phone system (http://www.dhs.state.il.us:8080/FSCalc/FSInputCalc.do?lang=en).  This is the java version that I'm replacing with the Flex one.  BTW, if you go there I DIDN'T have anything to do with the layout.  I inherited it and am stuck making the Flex application look like it.  This system works and outputs the stream via a servlet that is available to the phone system as a return value.  If Flex can't do the same, I have to learn how to call a java servlet.

                                 

                                But back to your question.  I'm still studying how to make the connection on how to move a value to the mxml application.  I had thought that I might be able to capture the error event on the object on the form.  Using a validator seemed easier (HA) at the time.  I am glad to have learned how to do it.  Any knowledge is a good thing and when you are a newbie like me there is more knowledge to gain than is known by me.

                                 

                                Jerry

                                • 13. Re: Newbie - how to use a validator
                                  Gus187 Level 1

                                  It seems a quite complex application but the flex community is pretty strong and I'm sure it'll help you if you have any other promblem

                                   

                                  Gus