示例#1
0
 /**
  * Iterate over the validation result to check for any validation failure. For the very first
  * validation failure, it reads the oneinc-resource.properties encapsulated in member variable
  * apps and set the user friendly message along with the IRC and throws the
  * OneIncValidationException.
  *
  * @param bean - Bean to be validated
  * @param results - contains the validation result
  * @param resources2 - ValidatorResources object prepared for validation-rules.xml
  * @param form - contains the mapping for field and their validation rule.
  * @throws CWValidationException - if validation gets fail.
  */
 @SuppressWarnings({"unchecked", "deprecation"})
 private void checkResults(
     Serializable bean, ValidatorResults results, ValidatorResources resources2, Form form)
     throws CWValidationException {
   Iterator propertyNames = results.getPropertyNames().iterator();
   while (propertyNames.hasNext()) {
     String propertyName = (String) propertyNames.next();
     ValidatorResult result = results.getValidatorResult(propertyName);
     Map actionMap = result.getActionMap();
     Iterator keys = actionMap.keySet().iterator();
     Field field = form.getField(propertyName);
     String prettyFieldName = apps.getString(field.getArg(0).getKey());
     while (keys.hasNext()) {
       String actName = (String) keys.next();
       ValidatorAction action = resources.getValidatorAction(actName);
       if (!result.isValid(actName)) {
         String message = apps.getString(action.getMsg());
         Object[] args = {prettyFieldName};
         String formatMsg = MessageFormat.format(message, args);
         int colonIndex = formatMsg.indexOf(',');
         String imfResponseCode = null;
         String msg = null;
         if (colonIndex > 0) {
           imfResponseCode = formatMsg.substring(0, colonIndex);
           msg = formatMsg.substring(colonIndex);
         }
         throw new CWValidationException(msg, imfResponseCode);
       }
     }
   }
 }
示例#2
0
  public int doStartTag() throws JspException {

    try {
      this.requestContext = new RequestContext((HttpServletRequest) this.pageContext.getRequest());
    } catch (RuntimeException ex) {
      throw ex;
    } catch (Exception ex) {
      pageContext.getServletContext().log("Exception in custom tag", ex);
    }

    // Look up this key to see if its a field of the current form
    boolean requiredField = false;
    boolean validationError = false;

    ValidatorResources resources = getValidatorResources();

    Locale locale = pageContext.getRequest().getLocale();

    if (locale == null) {
      locale = Locale.getDefault();
    }

    // get the name of the bean from the key
    String formName = key.substring(0, key.indexOf('.'));
    String fieldName = key.substring(formName.length() + 1);

    if (resources != null) {
      Form form = resources.getForm(locale, formName);

      if (form != null) {
        Field field = form.getField(fieldName);

        if (field != null) {
          if (field.isDependency("required") || field.isDependency("validwhen")) {
            requiredField = true;
          }
        }
      }
    }

    Errors errors = requestContext.getErrors(formName, false);
    List fes = null;
    if (errors != null) {
      fes = errors.getFieldErrors(fieldName);
      // String errorMsg = getErrorMessages(fes);
    }

    if (fes != null && fes.size() > 0) {
      validationError = true;
    }

    // Retrieve the message string we are looking for
    String message = null;
    try {
      message = getMessageSource().getMessage(key, null, locale);
    } catch (NoSuchMessageException nsm) {
      message = "???" + key + "???";
    }

    String cssClass = null;
    if (styleClass != null) {
      cssClass = styleClass;
    } else if (requiredField) {
      cssClass = "required";
    }

    String cssErrorClass = (errorClass != null) ? errorClass : "error";
    StringBuilder label = new StringBuilder();

    if ((message == null) || "".equals(message.trim())) {
      label.append("");
    } else {
      label.append("<label for=\"").append(fieldName).append("\"");

      if (cssClass != null) {
        label.append(" class=\"").append(cssClass);
        if (validationError) {
          label.append(" ").append(cssErrorClass);
        }
      }

      label.append("\">").append(message);
      label.append((requiredField) ? " <span class=\"required\">*</span>" : "");
      label.append((colon) ? ":" : "");
      label.append("</label>");
    }

    // Print the retrieved message to our output writer
    try {
      writeMessage(label.toString());
    } catch (IOException io) {
      io.printStackTrace();
      throw new JspException("Error writing label: " + io.getMessage());
    }

    // Continue processing this page
    return (SKIP_BODY);
  }