@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")) {