private static void _renderValidationScripts()

in trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/FormRenderer.java [618:929]


  private static void _renderValidationScripts(
    FacesContext     context,
    RenderingContext rc
    ) throws IOException
  {
    // if scripting isn't supported, no need to do the rest
    if (!supportsScripting(rc))
      return;

    //
    // Output validation-related JavaScript
    //
    ResponseWriter writer = context.getResponseWriter();
    CoreFormData   fData = (CoreFormData) rc.getFormData();

    // Fix up the form name for use as a Javascript identifier
    String jsID = XhtmlUtils.getJSIdentifier(fData.getName());

    writer.startElement("script", null);
    renderScriptDeferAttribute(context, rc);
    renderScriptTypeAttribute(context, rc);

    //
    // Write the array of client dependencies
    // Whether or not client side validation is enabled,
    // The dependencies may be needed - see bug
    // 4409339 TURNING OFF CLIENT SIDE VALIDATION CAUSES
    //                                ERRORS IN SELECTINPUTCOLOR & DATE
    List<String> clientDependencies = fData.getClientDependencies( false);
    if (clientDependencies != null)
    {
      for (int d = 0; d < clientDependencies.size(); d++)
      {
        writer.writeText(clientDependencies.get(d),null);
      }
    }

    RequestContext requestContext = RequestContext.getCurrentInstance();
    boolean isClientValidationDisabled =
      requestContext.getClientValidation() == RequestContext.ClientValidation.DISABLED;

    // Only bother writing out the function when there's no PPR,
    // as the content doesn't change request to request
    if (rc.getPartialPageContext() == null)
    {
      boolean isInline =
        (requestContext.getClientValidation() == RequestContext.ClientValidation.INLINE);

      //
      // write the validation function for this form
      //
      writer.writeText("function _", null);
      writer.writeText(jsID, null);

      writer.writeText("Validator(f,s){return ", null);

      if (isClientValidationDisabled)
      {
        // No client-side validation:  always return true
        writer.writeText("true", null);
      }
      else if (isInline)
      {
        writer.writeText("_validateInline(f,s)", null);
      }
      else
      {
        writer.writeText("_validateAlert(f,s,null,\"", null);
        writer.writeText(XhtmlUtils.escapeJS(
            rc.getTranslatedString(_GLOBAL_FORMAT_KEY)), null);
        writer.writeText("\",\"", null);

        writer.writeText(XhtmlUtils.escapeJS(
            rc.getTranslatedString("af_form.SUBMIT_ERRORS")), null);
        writer.writeText("\")", null);
      }

      writer.writeText(";}", null);
    }


    // If no client-side validation, return now
    if (isClientValidationDisabled)
    {
      writer.endElement("script");
      return;
    }

    //
    // Write the array of validation calls
    //
    //
    // Write the array of form validators
    //
    Map<String, List<CoreFormData.ConvertValidate>> validatorInfoMap =
      fData.getFormValidatorsInfo(false);

    if (validatorInfoMap != null)
    {
      writer.writeText("_addValidators(\"", null);
      writer.writeText(fData.getName(), null);
      writer.writeText("\",[", null);

      boolean firstFormInfo = true;
      // FIXME: the List here is wrong;  we should only ever have
      // one CoreFormData.ConvertValidate per ID, and anything else is
      // an error
      for (Map.Entry<String, List<CoreFormData.ConvertValidate>> validatorEntry  :
           validatorInfoMap.entrySet())
      {
        String clientId = validatorEntry.getKey();
        List<CoreFormData.ConvertValidate> validatorInfoList =
          validatorEntry.getValue();

        for (CoreFormData.ConvertValidate convertValidate : validatorInfoList)
        {

          if (firstFormInfo)
          {
            firstFormInfo = false;
          }
          else
          {
            // write the separator every time except the first time
            writer.writeText("],", null);
          }

          writer.writeText("\"", null);

          // write the element name of the element to be validated
          writer.writeText(clientId, null);
          writer.writeText("\",", null);

          // write out whether or not this element is required
          writer.writeText(convertValidate.required? "1" : "0", null);
          writer.writeText(",", null);

          if (convertValidate.requiredFormatIndex != null)
          {
            // write out the index of the required error message
            writer.writeText(convertValidate.requiredFormatIndex, null);
          }

          writer.writeText(",", null);

          Object converterInfo = convertValidate.converter;

          if (converterInfo != null)
          {
            writer.writeText(converterInfo, null);
          }
          else
          {
            writer.writeText("(void 0)", null);
          }

          writer.writeText(",[", null);

          ArrayList<Integer> validatorInfo = convertValidate.validators;

          if (validatorInfo != null)
          {
            boolean firstValidator = true;

            int i = 0;
            while (i < validatorInfo.size())
            {
              if (firstValidator)
              {
                firstValidator = false;
              }
              else
              {
                // write the separator every time except the first time
                writer.writeText(",", null);
              }

              // write the validation string for the validater
              writer.writeText(validatorInfo.get(i).toString(), null);

              i = i + 1;
            }
          }
        }
      }

      writer.writeText("]],[", null);

      Iterator<String> validationIterator = fData.getValidationIterator();
      if (validationIterator != null)
      {
        boolean firstValidation = true;

        while(validationIterator.hasNext())
        {
          String currValidation = validationIterator.next();

          if (firstValidation)
          {
            firstValidation = false;
          }
          else
          {
            // write the separator every time except the first time
            writer.writeText(",", null);
          }

          // write the error format
          // use single quotes since embedded single quotes
          // are automatically escaped
          writer.writeText("\'", null);
          writer.writeText(XhtmlUtils.escapeJS(currValidation), null);
          writer.writeText("\'", null);
        }
      }


      writer.writeText("],{", null);


      //
      // Render the labels used by validated fields in this form
      //

      // list of labels used for validation on this form
      List<String> inputList = fData.getValidatedInputList(false);

      int inputCount = (inputList != null)
                         ? inputList.size()
                         : 0;

      if (inputCount > 0)
      {
        Map<String, String> labelMap = fData.getLabelMap(false);

        if (labelMap != null)
        {
          boolean firstLabel = true;

          for (int i = 0; i < inputCount; i++)
          {
            String currID = inputList.get(i);

            // remove the ID entry to prevent multiple labels from
            // being written
            String currLabel = labelMap.remove(currID);

            if (currLabel != null)
            {
              if (firstLabel)
              {
                firstLabel = false;
              }
              else
              {
                // write the separator every time except the first time
                writer.writeText(",", null);
              }

              // write the ID of the validated field as the key
              writer.writeText("\'", null);
              writer.writeText(currID, null);
              writer.writeText("\':\'", null);

              // write the label of the validated field as the value
              writer.writeText(XhtmlUtils.escapeJS(currLabel), null);
              writer.writeText("\'", null);
            }
          }
        }
      }
      writer.writeText("},[", null);

      //
      // Render the error format list for this form
      //

      // list of error formats used for validation on this form
      Iterator<String> errorFormatIterator = fData.getErrorFormatIterator();

      if (errorFormatIterator != null)
      {
        boolean firstFormat = true;

        while(errorFormatIterator.hasNext())
        {
          String currErrorFormat = errorFormatIterator.next();

          if (firstFormat)
          {
            firstFormat = false;
          }
          else
          {
            // write the separator every time except the first time
            writer.writeText(",", null);
          }

          // write the error format
          writer.writeText("'", null);
          writer.writeText(XhtmlUtils.escapeJS(currErrorFormat), null);
          writer.writeText("'", null);
        }
      }

      writer.writeText("]);", null);
    }

    _renderSubformLists(context, jsID);

    writer.endElement("script");
  }