1 Reply Latest reply on Feb 18, 2010 11:04 PM by archemedia

    Form validation annoyance (help)

    joshy_888

      Hey guys I'm having what seems to be a simple but very very annoying issue with my form submission and validation.
      I'll use one field from the form for my example

       

      I have a validator on a text box in a form:

       

      //the validator
      <mx:StringValidator id="nameValidate" source="{name}" property="text" required="true" />
      
      //the text input box in the form
      <mx:TextInput id="name"/>
      
      //the button to submit the form
      <mx:Button label="Add" id="addbutton" click="validateAndSubmit()" />
      
      //In script tags 
      //the validation function to check that there is text in the text box
      public function validateAndSubmit():void {
                      import mx.controls.Alert;
                      import mx.validators.Validator;
                          
                      var errorArray:Array = Validator.validateAll([nameValidate]);
                      if(errorArray.length == 0) {
                          send_data();
                      }else{
                          Alert.show("There is an error in the form","Data input Error");
                      }
                  }
      
      //the send function that inserts data into the database if the validation is a succecss
      public function send_data():void {
                      amfcall.addData(name.text);
                      name.text="";
                  }
      

       

      Now, when a user clicks the text input box and then clicks out of it, the box will then become outlined in red and state that it is a required field. Same happens if the user clicks the submit button without typing anything into the text box (submitting an empty form).

       

      The issue is: If the user does put text into the box and successfully submits it, the send_data function then automatically clears the name.text box. But the problem is that once this happens, the box becomes instantly outlined in red again with the validation error stating that the field is required. I don't want this to happen after the user submits their data! It should only happen when they try to submit an empty form!