Package org.apache.commons.validator

Examples of org.apache.commons.validator.Form


        ValidatorResources resources) {
           
        boolean success = true;

        // Start by getting the form for the current locale and Bean.
        Form form = resources.get(Locale.getDefault(), "ValidateBean");

        System.out.println("\n\nValidating:");
        System.out.println(bean);

        // Iterate over each of the properties of the Bean which had messages.
        Iterator propertyNames = results.get();
        while (propertyNames.hasNext()) {
            String propertyName = (String) propertyNames.next();

            // Get the Field associated with that property in the Form
            Field field = (Field) form.getFieldMap().get(propertyName);

            // Look up the formatted name of the field from the Field arg0
            String prettyFieldName = apps.getString(field.getArg0().getKey());

            // Get the result of validating the property.
View Full Code Here


                + ValidatorPlugIn.VALIDATOR_KEY + config.getPrefix() + "\"");
        }       

        Locale locale = TagUtils.getInstance().getUserLocale(this.pageContext, null);

        Form form = resources.getForm(locale, formName);

        if ("true".equalsIgnoreCase(dynamicJavascript) && form == null)
        {
            throw new JspException("No form found under '"
                                   + formName
View Full Code Here

                + ValidatorPlugIn.VALIDATOR_KEY + config.getPrefix() + "\"");
        }       

        Locale locale = TagUtils.getInstance().getUserLocale(this.pageContext, null);

        Form form = resources.getForm(locale, formName);

        if ("true".equalsIgnoreCase(dynamicJavascript) && form == null)
        {
            throw new JspException("No form found under '"
                                   + formName
View Full Code Here

                ValidatorPlugIn.VALIDATOR_KEY + config.getPrefix(),
                PageContext.APPLICATION_SCOPE);

        Locale locale = TagUtils.getInstance().getUserLocale(pageContext, null);

        Form form = resources.getForm(locale, formName);
        if (form != null) {
            if ("true".equalsIgnoreCase(dynamicJavascript)) {
                MessageResources messages =
                    (MessageResources) pageContext.getAttribute(
                        bundle + config.getPrefix(),
                        PageContext.APPLICATION_SCOPE);

                List lActions = new ArrayList();
                List lActionMethods = new ArrayList();

                // Get List of actions for this Form
                for (Iterator i = form.getFields().iterator(); i.hasNext();) {
                    Field field = (Field) i.next();

                    for (Iterator x = field.getDependencyList().iterator(); x.hasNext();) {
                        Object o = x.next();

                        if (o != null && !lActionMethods.contains(o)) {
                            lActionMethods.add(o);
                        }
                    }

                }

                // Create list of ValidatorActions based on lActionMethods
                for (Iterator i = lActionMethods.iterator(); i.hasNext();) {
                    String depends = (String) i.next();
                    ValidatorAction va = resources.getValidatorAction(depends);

                    // throw nicer NPE for easier debugging
                    if (va == null) {
                        throw new NullPointerException(
                            "Depends string \""
                                + depends
                                + "\" was not found in validator-rules.xml.");
                    }              
                   
                    String javascript = va.getJavascript();
                    if (javascript != null && javascript.length() > 0) {
                        lActions.add(va);
                    } else {
                        i.remove();
                    }
                }

                Collections.sort(lActions, new Comparator() {
                    public int compare(Object o1, Object o2) {
                        ValidatorAction va1 = (ValidatorAction) o1;
                        ValidatorAction va2 = (ValidatorAction) o2;

                        if ((va1.getDepends() == null || va1.getDepends().length() == 0)
                            && (va2.getDepends() == null || va2.getDepends().length() == 0)) {
                            return 0;
                        } else if (
                            (va1.getDepends() != null && va1.getDepends().length() > 0)
                                && (va2.getDepends() == null || va2.getDepends().length() == 0)) {
                            return 1;
                        } else if (
                            (va1.getDepends() == null || va1.getDepends().length() == 0)
                                && (va2.getDepends() != null && va2.getDepends().length() > 0)) {
                            return -1;
                        } else {
                            return va1.getDependencyList().size() - va2.getDependencyList().size();
                        }
                    }
                });

                String methods = null;
                for (Iterator i = lActions.iterator(); i.hasNext();) {
                    ValidatorAction va = (ValidatorAction) i.next();

                    if (methods == null) {
                        methods = va.getMethod() + "(form)";
                    } else {
                        methods += " && " + va.getMethod() + "(form)";
                    }
                }

                results.append(getJavascriptBegin(methods));

                for (Iterator i = lActions.iterator(); i.hasNext();) {
                    ValidatorAction va = (ValidatorAction) i.next();
                    String jscriptVar = null;
                    String functionName = null;

                    if (va.getJsFunctionName() != null && va.getJsFunctionName().length() > 0) {
                        functionName = va.getJsFunctionName();
                    } else {
                        functionName = va.getName();
                    }

                    if (isStruts11()) {
                        results.append("    function " +
                                       functionName + " () { \n");
                    } else {
                        results.append("    function " +
                                       formName + "_" + functionName + " () { \n");
                    }
                    for (Iterator x = form.getFields().iterator(); x.hasNext();) {
                        Field field = (Field) x.next();

                        // Skip indexed fields for now until there is a good way to handle
                        // error messages (and the length of the list (could retrieve from scope?))
                        if (field.isIndexed()
View Full Code Here

     * @param locale <code>Locale</code> to search under.
     * @return <code>true</code> if this validator factory can create a validator for
     *         the given bean name, <code>false otherwise.
     */
    public boolean hasRulesForBean(String beanName, Locale locale) {
        Form form = validatorResources.getForm(locale, beanName);
        return (form != null);
    }
View Full Code Here

        if (locale == null) {
            locale = defaultLocale;
        }

        Form form = null;
        form = resources.get(locale, formName);
        if (form != null) {
            if ("true".equals(dynamicJavascript)) {
                MessageResources messages =
                    (MessageResources) pageContext.getAttribute(
                        bundle + config.getPrefix(),
                        PageContext.APPLICATION_SCOPE);

                List lActions = new ArrayList();
                List lActionMethods = new ArrayList();

                // Get List of actions for this Form
                for (Iterator i = form.getFields().iterator(); i.hasNext();) {
                    Field field = (Field) i.next();

                    for (Iterator x = field.getDependencies().iterator(); x.hasNext();) {
                        Object o = x.next();

                        if (o != null && !lActionMethods.contains(o)) {
                            lActionMethods.add(o);
                        }
                    }

                }

                // Create list of ValidatorActions based on lActionMethods
                for (Iterator i = lActionMethods.iterator(); i.hasNext();) {
                    String depends = (String) i.next();
                    ValidatorAction va = resources.getValidatorAction(depends);

                    // throw nicer NPE for easier debugging
                    if (va == null) {
                        throw new NullPointerException(
                            "Depends string \""
                                + depends
                                + "\" was not found in validator-rules.xml.");
                    }              
                   
                    String javascript = va.getJavascript();
                    if (javascript != null && javascript.length() > 0) {
                        lActions.add(va);
                    } else {
                        i.remove();
                    }
                }

                Collections.sort(lActions, new Comparator() {
                    public int compare(Object o1, Object o2) {
                        ValidatorAction va1 = (ValidatorAction) o1;
                        ValidatorAction va2 = (ValidatorAction) o2;

                        if ((va1.getDepends() == null || va1.getDepends().length() == 0)
                            && (va2.getDepends() == null || va2.getDepends().length() == 0)) {
                            return 0;
                        } else if (
                            (va1.getDepends() != null && va1.getDepends().length() > 0)
                                && (va2.getDepends() == null || va2.getDepends().length() == 0)) {
                            return 1;
                        } else if (
                            (va1.getDepends() == null || va1.getDepends().length() == 0)
                                && (va2.getDepends() != null && va2.getDepends().length() > 0)) {
                            return -1;
                        } else {
                            return va1.getDependencies().size() - va2.getDependencies().size();
                        }
                    }
                });

                String methods = null;
                for (Iterator i = lActions.iterator(); i.hasNext();) {
                    ValidatorAction va = (ValidatorAction) i.next();

                    if (methods == null) {
                        methods = va.getMethod() + "(form)";
                    } else {
                        methods += " && " + va.getMethod() + "(form)";
                    }
                }

                results.append(getJavascriptBegin(methods));

                for (Iterator i = lActions.iterator(); i.hasNext();) {
                    ValidatorAction va = (ValidatorAction) i.next();
                    String jscriptVar = null;
                    String functionName = null;

                    if (va.getJsFunctionName() != null && va.getJsFunctionName().length() > 0) {
                        functionName = va.getJsFunctionName();
                    } else {
                        functionName = va.getName();
                    }

                    results.append("    function " + functionName + " () { \n");
                    for (Iterator x = form.getFields().iterator(); x.hasNext();) {
                        Field field = (Field) x.next();

                        // Skip indexed fields for now until there is
                        // a good way to handle error messages (and the length of the list (could retrieve from scope?))
                        if (!field.isIndexed()
View Full Code Here

       }
      
       if (locale == null)
           locale = defaultLocale;

       Form form = null;
       if ((form = resources.get(locale, formName)) != null && "true".equals(dynamicJavascript)) {
          MessageResources messages = (MessageResources)
             pageContext.getAttribute(bundle, PageContext.APPLICATION_SCOPE);
         
          List lActions = new ArrayList();
          List lActionMethods = new ArrayList();
         
          // Get List of actions for this Form
          for (Iterator i = form.getFields().iterator(); i.hasNext(); ) {
             Field field = (Field)i.next();
         
             for (Iterator x = field.getDependencies().iterator(); x.hasNext(); ) {  
               Object o = x.next();
              
               if (o != null && !lActionMethods.contains(o))
                   lActionMethods.add(o);
             }
            
          }
         
          // Create list of ValidatorActions based on lActionMethods
          for (Iterator i = lActionMethods.iterator(); i.hasNext(); ) {
             ValidatorAction va = resources.getValidatorAction((String)i.next());
            
             String javascript = va.getJavascript();
             if (javascript != null && javascript.length() > 0)
                lActions.add(va);
             else
                i.remove();
          }
         
          Collections.sort(lActions, new Comparator() {
             public int compare(Object o1, Object o2) {
                ValidatorAction va1 = (ValidatorAction)o1;
                ValidatorAction va2 = (ValidatorAction)o2;
         
           if ((va1.getDepends() == null || va1.getDepends().length() == 0) &&
                 (va2.getDepends() == null || va2.getDepends().length() == 0)) {
                   return 0;
                } else if ((va1.getDepends() != null && va1.getDepends().length() > 0) &&
                           (va2.getDepends() == null || va2.getDepends().length() == 0)) {
                   return 1;
                } else if ((va1.getDepends() == null || va1.getDepends().length() == 0) &&
                         (va2.getDepends() != null && va2.getDepends().length() > 0)) {
                   return -1;
                } else {
                   return va1.getDependencies().size() - va2.getDependencies().size();
                }
             }
          });      
         
          String methods = null;
          for (Iterator i = lActions.iterator(); i.hasNext(); ) {
             ValidatorAction va = (ValidatorAction)i.next();
            
             if (methods == null)
                methods = va.getMethod() + "(form)";
             else
                methods += " && " + va.getMethod() + "(form)";
          }
         
          results.append(getJavascriptBegin(methods));
         
          for (Iterator i = lActions.iterator(); i.hasNext(); ) {
             ValidatorAction va = (ValidatorAction)i.next();
             String jscriptVar = null;
             String functionName = null;
            
             if (va.getJsFunctionName() != null && va.getJsFunctionName().length() > 0)
                functionName = va.getJsFunctionName();
             else
                functionName = va.getName();
            
             results.append("   function " + functionName + " () { \n");
             for (Iterator x = form.getFields().iterator(); x.hasNext(); ) {
                Field field = (Field)x.next();        

                // Skip indexed fields for now until there is
                // a good way to handle error messages (and the length of the list (could retrieve from scope?))
                if (!field.isIndexed() && field.getPage() == page && field.isDependency(va.getName())) {
View Full Code Here

      
       if (locale == null) {
           locale = defaultLocale;
       }

       Form form = null;
       if ((form = resources.get(locale, formName)) != null && "true".equals(dynamicJavascript)) {
          MessageResources messages = (MessageResources)
             pageContext.getAttribute(bundle, PageContext.APPLICATION_SCOPE);
         
          List lActions = new ArrayList();
          List lActionMethods = new ArrayList();
         
          // Get List of actions for this Form
          for (Iterator i = form.getFields().iterator(); i.hasNext(); ) {
             Field field = (Field)i.next();
         
             for (Iterator x = field.getDependencies().iterator(); x.hasNext(); ) {  
               Object o = x.next();
              
               if (o != null && !lActionMethods.contains(o)) {
                   lActionMethods.add(o);
                }
             }
            
          }
         
          // Create list of ValidatorActions based on lActionMethods
          for (Iterator i = lActionMethods.iterator(); i.hasNext(); ) {
             ValidatorAction va = resources.getValidatorAction((String)i.next());
            
             String javascript = va.getJavascript();
             if (javascript != null && javascript.length() > 0) {
                lActions.add(va);
             } else {
                i.remove();
             }
          }
         
          Collections.sort(lActions, new Comparator() {
             public int compare(Object o1, Object o2) {
                ValidatorAction va1 = (ValidatorAction)o1;
                ValidatorAction va2 = (ValidatorAction)o2;
         
           if ((va1.getDepends() == null || va1.getDepends().length() == 0) &&
                 (va2.getDepends() == null || va2.getDepends().length() == 0)) {
                   return 0;
                } else if ((va1.getDepends() != null && va1.getDepends().length() > 0) &&
                           (va2.getDepends() == null || va2.getDepends().length() == 0)) {
                   return 1;
                } else if ((va1.getDepends() == null || va1.getDepends().length() == 0) &&
                         (va2.getDepends() != null && va2.getDepends().length() > 0)) {
                   return -1;
                } else {
                   return va1.getDependencies().size() - va2.getDependencies().size();
                }
             }
          });      
         
          String methods = null;
          for (Iterator i = lActions.iterator(); i.hasNext(); ) {
             ValidatorAction va = (ValidatorAction)i.next();
            
             if (methods == null) {
                methods = va.getMethod() + "(form)";
             } else {
                methods += " && " + va.getMethod() + "(form)";
             }
          }
         
          results.append(getJavascriptBegin(methods));
         
          for (Iterator i = lActions.iterator(); i.hasNext(); ) {
             ValidatorAction va = (ValidatorAction)i.next();
             String jscriptVar = null;
             String functionName = null;
            
             if (va.getJsFunctionName() != null && va.getJsFunctionName().length() > 0) {
                functionName = va.getJsFunctionName();
             } else {
                functionName = va.getName();
             }
            
             results.append("   function " + functionName + " () { \n");
             for (Iterator x = form.getFields().iterator(); x.hasNext(); ) {
                Field field = (Field)x.next();        

                // Skip indexed fields for now until there is
                // a good way to handle error messages (and the length of the list (could retrieve from scope?))
                if (!field.isIndexed() && field.getPage() == page && field.isDependency(va.getName())) {
View Full Code Here

  @SuppressWarnings("unchecked")
  public static String generateValidationJs(String formName) {
    StringBuilder totleJs = new StringBuilder("(function($){$().ready(function() {\n");
    StringBuilder rulesJs = new StringBuilder("rules:{\n");
    StringBuilder messagesJs = new StringBuilder("messages:{\n");
    Form form = validatorResources.getForm(Locale.getDefault(), formName);

    int fieldIndex = 0;
    List<Field> fields = form.getFields();
    for (Field field : fields) {
      if (fieldIndex != 0) {
        rulesJs.append(",\n");
        messagesJs.append(",\n");
      }
      rulesJs.append(field.getProperty()).append(":{");
      messagesJs.append(field.getProperty()).append(":{");

      int dependencyIndex = 0;
      List<String> dependencyList = field.getDependencyList();
      for (String dependency : dependencyList) {
        if (dependencyIndex != 0) {
          rulesJs.append(',');
          messagesJs.append(',');
        }
        ValidatorAction va = validatorResources.getValidatorAction(dependency);
        String message = ValidationUtils.getMessage(va, field);

        if (dependency.equals("required")) {
          rulesJs.append("required:true");
          messagesJs.append("required:'").append(message).append('\'');
        }

        if (dependency.equals("minlength")) {
          try {
            int min = Integer.parseInt(field.getVarValue("minlength"));
            rulesJs.append("minlength:").append(min);
            messagesJs.append("minlength:'").append(message).append('\'');
          } catch (Exception e) {
            LOGGER.info("生成" + form.getName() + "的" + field.getProperty()
                + "字段的minlength校验失败", e);
          }

        }

        if (dependency.equals("maxlength")) {
          try {
            int max = Integer.parseInt(field.getVarValue("maxlength"));
            rulesJs.append("maxlength:").append(max);
            messagesJs.append("maxlength:'").append(message).append('\'');
          } catch (Exception e) {
            LOGGER.info("生成" + form.getName() + "的" + field.getProperty()
                + "字段的minlength校验失败", e);
          }
        }

        if (dependency.equals("mask")) {
          String mask = field.getVarValue("mask");
          rulesJs.append("regexmatch:").append('/').append(mask).append('/');
          messagesJs.append("regexmatch:'").append(message).append('\'');
        }

        if (dependency.equals("byte") || dependency.equals("short")
            || dependency.equals("integer") || dependency.equals("long")
            || dependency.equals("double") || dependency.equals("float")) {
          // FIXME:全部归为数值型,实际应用中是否需要区分
          rulesJs.append("number:true");
          messagesJs.append("number:'").append(message).append('\'');
        }

        if (dependency.equals("date")) {
          // FIXME:使用正则,不再使用日期类型
        }

        if (dependency.toLowerCase().endsWith("range")) {
          try {
            int min = Integer.parseInt(field.getVarValue("min"));
            int max = Integer.parseInt(field.getVarValue("max"));
            rulesJs.append("range:").append('[').append(min).append('\'').append(max)
                .append(']');
            messagesJs.append("range:'").append(message).append('\'');
          } catch (Exception e) {
            LOGGER.info("生成" + form.getName() + "的" + field.getProperty() + "字段的"
                + dependency + "校验失败", e);
          }
        }

        // if (dependency.endsWith("email")) {
        // // FIXME:使用正则,不再使用邮件类型
        // }

        if (dependency.endsWith("notEqualTo")) {
          try {
            String toId = field.getVarValue("toid");
            rulesJs.append("notEqualTo:").append("\"#").append(formName)
                .append(" [name='").append(toId).append("']\"");
            messagesJs.append("notEqualTo:'").append(message).append('\'');
          } catch (Exception e) {
            LOGGER.info("生成" + form.getName() + "的" + field.getProperty()
                + "字段的notEqualTo校验失败", e);
          }
        }

        if (dependency.endsWith("equalTo")) {
          try {
            String toId = field.getVarValue("toid");
            rulesJs.append("equalTo:").append("\"#").append(formName)
                .append(" [name='").append(toId).append("']\"");
            messagesJs.append("equalTo:'").append(message).append('\'');
          } catch (Exception e) {
            LOGGER.info("生成" + form.getName() + "的" + field.getProperty()
                + "字段的notEqualTo校验失败", e);
          }
        }

        // if (dependency.endsWith("submitToken")) {
View Full Code Here

  public boolean hasRulesForBean(String beanName) {
    return hasRulesForBean(beanName, Locale.getDefault());
  }

  public boolean hasRulesForBean(String beanName, Locale locale) {
    Form form = validatorResources.getForm(locale, beanName);
    return (form != null);
  }
View Full Code Here

TOP

Related Classes of org.apache.commons.validator.Form

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.