function _multiValidate()

in trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/Core.js [2514:2715]


function _multiValidate(
  form,
  source,
  validators
  )
{
  // Initialise the return map.
  var failureMap = new Object();

  var subforms = window[_getFormName(form) + "_SF"];
  var ignorePrefixes = new Array();
  var foundUsedSubform = false;
  var key;
  if (source != (void 0))
  {
    // Find if there's any prefix that matches
    for (key in subforms)
    {
      if (source.indexOf(key + ":") == 0)
      {
        foundUsedSubform = true;
        break;
      }
    }

    // Build up all prefixes that don't match
    for (key in subforms)
    {
      if (source.indexOf(key + ":") != 0)
      {
        if ((foundUsedSubform) || (subforms[key] == 1))
          ignorePrefixes.push(key + ":");
      }
    }
  }

  // We check for any relevent validation failures here, not just validations.
  // If a validation has been run on one field in the form (e.g. an onBlur), we
  // still need to run every other validation. However, if that one validation
  // failed, the user has seen one alert, don't bug them with a second til they
  // have fixed the first error.
  if (validators && !_recentValidation(true))
  {
    for (var id in validators)
    {
      if(_getElementById(document, id) == null)
      {
          continue;
      }

      var isIgnored = false;
      // If this field is one that's specifically being ignored,
      // then don't validate here.
      for (var j = 0; j < ignorePrefixes.length; j++)
      {
        if (id.indexOf(ignorePrefixes[j]) == 0)
        {
          isIgnored = true;
          break;
        }
      }

      if (isIgnored)
        continue;

      // get the current form element to validate
      var currInput = _getFormElement(form, id);

      // Make sure we have a non-null input control.  It is possible
      // that in rich client environments the DOM for the input
      // control may have been temporarily removed from the document.
      // If we don't find DOM for the current input, move on to the
      // next input.

      // todo: Should also check for visibility of currInput, since
      //       rich client may have "hidden" the input, in which case
      //       validation shouldn't fire.
      if (!currInput)
        continue;

      //Initialize the failure array for this input
      var inputFailures = new Array();

      var descriptor = validators[id];
      var label = descriptor.label;

      // if currInput is an array then multiple elements have the same name.
      // Only the first will be validated as subsequent values should be in sync
      var elementType = currInput.type;

      if (!elementType && currInput.length)
      {
        var firstType = currInput[0].type;
        if (firstType != "radio" && firstType != "checkbox")
        {
          currInput = currInput[0];
        }
      }

      var value = _getValue(currInput);
      var required = descriptor.required;
      if ( required && ((value == "" ) || (value == null)))
      {

        // get the formatted error string for the current input and
        var requiredErrorString = _getErrorString(currInput, label,
                                                  descriptor.requiredFormat);

        // Populate the failureMap with the current error
        inputFailures[inputFailures.length] =
            new TrFacesMessage(requiredErrorString, requiredErrorString);
      }
      else
      {
        var converterConstructor = descriptor.converter;

        // set the converterError var to false for each input, otherwise nothing
        // after the first conversion error is validated
        var converterError = false;

        if ( converterConstructor )
        {

          // do the conversion if this element has a value
          if ((value != null) &&
              !((typeof value == "string") && (value == "")))
          {
            var converter = eval(converterConstructor);
            try
            {
              value = converter.getAsObject(value, label);
            }
            catch (e)
            {
              converterError = true;
              // Populate the failureMap with the current error
             if (_agent.isPIE || _agent.isNokiaPhone || _agent.isBlackBerry)
             {
               inputFailures[inputFailures.length] = e.message;
             }
             else
             {
               inputFailures[inputFailures.length] = e.getFacesMessage();
             }
            }
          }
        }

        if ( converterError == false)
        {
          var validatorArray = descriptor.validators;
          if (validatorArray)
          {
            for ( var j = 0; j < validatorArray.length; j = j + 1)
            {
              // do the validation if this element has a value
              // Don't just compare against "", since the value has
              // already been converted to a non-string type
              if ((value !== null) &&
                  !((typeof value == "string") && value == ""))
              {
                // evaluate the validator
                var validatorConstructor = validatorArray[j];
                if (validatorConstructor && value !== undefined)
                {
                  var validator = eval(validatorConstructor);

                  try
                  {
                    validator.validate(value, label, converter);
                  }
                  catch (e)
                  {
                    // Populate the failureMap with the current error
                    if (_agent.isPIE || _agent.isNokiaPhone || _agent.isBlackBerry)
                    {
                      inputFailures[inputFailures.length] = e.message;
                    }
                    else
                    {
                      inputFailures[inputFailures.length] = e.getFacesMessage();
                    }
                  }
                }
              }
            }
          }
        }
      }

      // if there were failures, then add the current input to the failuresMap
      if (inputFailures.length > 0)
      {
        // TRINIDAD-123: Use input 'name' from validators array rather than currInput.id
        // to avoid issues with radio buttons having numeric id suffixes
        failureMap[id] = inputFailures;
      }
    }
  }

  return failureMap;
}