DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 11.18 Validating a Multipage Form

11.18.1 Problem

You want to validate elements within a multipage form.

11.18.2 Solution

Create and invoke a validate( ) method for the custom MultiPageForm class.

11.18.3 Discussion

You can create a MultiPageForm.validate( ) method that leverages the Form.validate( ) method and works in much the same way. Add the following code to your Form.as file for easy inclusion in other projects:

MultiPageForm.prototype.validate = function (  ) {
  var vRes;

  // Loop through all the form pages.
  for (var i = 0; i < this.forms.length; i++) {

    // Call the validate(  ) method for each page of the form. If validate(  ) returns 
    // something other than true, some form element didn't validate. In that case,
    // display that page of the form and return the name of the offending element.
    vRes = this.forms[i].validate(  );
    if (!vRes) {
      this.setPage(i + 1);
      return vRes;
    }
  }
  return true;
};

You can use the validate( ) method for multipage forms as you did for single-page forms:

submitBtn_pb.onRelease = function (  ) {
  // Call the validate(  ) method of the multipage form.
  var valid = _root.myMultiForm.validate(  );
 
  // If the form validates, submit the form. Otherwise, display the name of the form
  // element that needs to be corrected. If an element fails to validate, the
  // multipage form automatically displays the page with the problem.
  if (valid) {
    // Submit form
  } else {
    trace(valid);
  }
};

11.18.4 See Also

Recipe 11.14 and Recipe 11.15

    [ Team LiB ] Previous Section Next Section