Java tutorial
/* * SurveyPanel * Copyright (C) 2009 Serge Tan Panza * All rights reserved. * License: GNU/GPL License v3 , see LICENSE.txt * SurveyPanel is free software. This version may have been modified pursuant * to the GNU General Public License, and as distributed it includes or * is derivative of works licensed under the GNU General Public License or * other free or open source software licenses. * See COPYRIGHT.txt for copyright notices and details. * */ package com.surveypanel.form.validation; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import org.apache.commons.validator.GenericValidator; import com.surveypanel.form.FormData; import com.surveypanel.form.QuestionImpl; /** * @author stanpanza * */ public class RequiredValidator extends Validator { @Override public String getDefaultErrorKey() { return "form.field.required"; } @Override public void validate(QuestionImpl question, FormData formData, Locale locale, Map<String, String> config, ValidationResult validationResult) throws Exception { Integer maximum = 0; Integer minimum = 0; String error_min = config.containsKey("error_min") ? config.get("error_min") : "form.error.min"; String error_max = config.containsKey("error_max") ? config.get("error_max") : "form.error.max"; String error_req = config.containsKey("error_req") ? config.get("error_req") : getDefaultErrorKey(); String min = config.get("min"); String max = config.get("max"); if (max != null) maximum = Integer.parseInt(max); if (min != null) minimum = Integer.parseInt(min); if (question.isMulti()) { Map<String, List<String>> errorFields = new HashMap<String, List<String>>(); Map<String, List<String>> validFieldNames = new HashMap<String, List<String>>(); FormData[] values = (FormData[]) formData.getValue(); for (FormData data : values) { String[] splitFieldName = data.getName().split("_"); Map<String, List<String>> targetMap = validFieldNames; if (checkEmptyString((String) data.getValue())) { targetMap = errorFields; List<String> removed = validFieldNames.remove(splitFieldName[0]); } if (splitFieldName.length == 2) { List<String> list = targetMap.get(splitFieldName[0]); if (list == null) { list = new ArrayList<String>(); list.add(splitFieldName[1]); targetMap.put(splitFieldName[0], list); } else { list.add(splitFieldName[1]); } } else { List<String> list = new ArrayList<String>(); list.add(data.getName()); targetMap.put(data.getName(), list); } } if (minimum > 0 && validFieldNames.size() < minimum) { validationResult.addError( qDefinition.getText(error_min, new Object[] { question.getName(), min, minimum }, locale)); } if (maximum > 0 && validFieldNames.size() > maximum) { validationResult.addError( qDefinition.getText(error_max, new Object[] { question.getName(), min, maximum }, locale)); } if (maximum == 0 && minimum == 0 && errorFields.size() > 0) { Map<String, List<String>> errorFieldsCopy = new HashMap<String, List<String>>(); errorFieldsCopy.putAll(errorFields); for (String key : errorFields.keySet()) { if (errorFieldsCopy.containsKey(key) || !validFieldNames.containsKey(key)) { validationResult.addError(key, qDefinition.getText(error_req, new Object[] { key }, locale)); } else { errorFieldsCopy.remove(key); } } if (!errorFieldsCopy.isEmpty()) { validationResult .addError(qDefinition.getText(error_req, new Object[] { question.getName() }, locale)); } } } else { if (checkEmptyString((String) formData.getValue())) { validationResult .addError(qDefinition.getText(error_req, new Object[] { question.getName() }, locale)); } } } private boolean checkEmptyString(String value) { return GenericValidator.isBlankOrNull(value); } }