List of usage examples for org.apache.commons.validator Form getFields
public List<Field> getFields()
List
of Field
s is returned as an unmodifiable List
. From source file:us.mn.state.health.lims.taglib.JavascriptValidatorTag.java
/** * Generates the dynamic JavaScript for the form. * @param config/*from w ww. j a v a 2 s . c om*/ * @param resources * @param locale * @param form */ private String createDynamicJavascript(ModuleConfig config, ValidatorResources resources, Locale locale, Form form) throws JspException { StringBuffer results = new StringBuffer(); MessageResources messages = TagUtils.getInstance().retrieveMessageResources(pageContext, bundle, true); HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); ServletContext application = pageContext.getServletContext(); List actions = this.createActionList(resources, form); final String methods = this.createMethods(actions, this.stopOnError(config)); String formName = form.getName(); jsFormName = formName; if (jsFormName.charAt(0) == '/') { String mappingName = TagUtils.getInstance().getActionMappingName(jsFormName); ActionMapping mapping = (ActionMapping) config.findActionConfig(mappingName); if (mapping == null) { JspException e = new JspException(messages.getMessage("formTag.mapping", mappingName)); pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE); throw e; } jsFormName = mapping.getAttribute(); } results.append(this.getJavascriptBegin(methods)); for (Iterator i = actions.iterator(); i.hasNext();) { ValidatorAction va = (ValidatorAction) i.next(); int jscriptVar = 0; String functionName = null; if (va.getJsFunctionName() != null && va.getJsFunctionName().length() > 0) { functionName = va.getJsFunctionName(); } else { functionName = va.getName(); } results.append(" function " + jsFormName + "_" + 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())) { continue; } String message = Resources.getMessage(application, request, messages, locale, va, field); message = (message != null) ? message : ""; // prefix variable with 'a' to make it a legal identifier results.append(" this.a" + jscriptVar++ + " = new Array(\"" + field.getKey() + "\", \"" + escapeQuotes(message) + "\", "); results.append("new Function (\"varName\", \""); Map vars = field.getVars(); // Loop through the field's variables. Iterator varsIterator = vars.keySet().iterator(); while (varsIterator.hasNext()) { String varName = (String) varsIterator.next(); Var var = (Var) vars.get(varName); String varValue = null; //bugzilla 1512 if (varName.startsWith("datePattern")) { if (var.getValue().equals("date.format.validate")) { String messageKey = var.getValue(); varValue = ResourceLocator.getInstance().getMessageResources().getMessage(locale, messageKey); } else { varValue = var.getValue(); } } else { varValue = var.getValue(); } String jsType = var.getJsType(); // skip requiredif variables field, fieldIndexed, fieldTest, fieldValue if (varName.startsWith("field")) { continue; } String varValueEscaped = ValidatorUtils.replace(varValue, "\\", "\\\\"); varValueEscaped = ValidatorUtils.replace(varValueEscaped, "\"", "\\\""); if (Var.JSTYPE_INT.equalsIgnoreCase(jsType)) { results.append("this." + varName + "=" + varValueEscaped + "; "); } else if (Var.JSTYPE_REGEXP.equalsIgnoreCase(jsType)) { results.append("this." + varName + "=/" + varValueEscaped + "/; "); } else if (Var.JSTYPE_STRING.equalsIgnoreCase(jsType)) { results.append("this." + varName + "='" + varValueEscaped + "'; "); // So everyone using the latest format doesn't need to change their xml files immediately. } else if ("mask".equalsIgnoreCase(varName)) { results.append("this." + varName + "=/" + varValueEscaped + "/; "); } else { results.append("this." + varName + "='" + varValueEscaped + "'; "); } } results.append(" return this[varName];\"));\n"); } results.append(" } \n\n"); } return results.toString(); }
From source file:us.mn.state.health.lims.taglib.JavascriptValidatorTag.java
/** * Get List of actions for the given Form. * @param resources/*from w w w . jav a 2 s .c o m*/ * @param form * @return A sorted List of ValidatorAction objects. */ private List createActionList(ValidatorResources resources, Form form) { List actionMethods = new ArrayList(); Iterator iterator = form.getFields().iterator(); while (iterator.hasNext()) { Field field = (Field) iterator.next(); for (Iterator x = field.getDependencyList().iterator(); x.hasNext();) { Object o = x.next(); if (o != null && !actionMethods.contains(o)) { actionMethods.add(o); } } } List actions = new ArrayList(); // Create list of ValidatorActions based on actionMethods iterator = actionMethods.iterator(); while (iterator.hasNext()) { String depends = (String) iterator.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."); } if (va.getJavascript() != null && va.getJavascript().length() > 0) { actions.add(va); } else { iterator.remove(); } } Collections.sort(actions, actionComparator); return actions; }