package jp.co.flect.formvalidation;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.ArrayList;

public class ValidationResult {
	
	private LinkedHashMap<FormItem, List<String>> errors = new LinkedHashMap<FormItem, List<String>>();
	private List<String> commonErrors = new ArrayList<String>();
	
	public void addError(FormItem item, List<String> msgs) {
		this.errors.put(item, msgs);
	}
	
	public void addCommonError(String msg) {
		if (!this.commonErrors.contains(msg)) {
			this.commonErrors.add(msg);
		}
	}
	
	public LinkedHashMap<FormItem, List<String>> getErrors() { return this.errors;}
	public List<String> getCommonErrors() { return this.commonErrors;}
	
	public boolean hasErrors() { return this.errors.size() + this.commonErrors.size() > 0;}
	
	public boolean isSuccess() { return !hasErrors();}
}

@@ -62,16 +62,38 @@
 ashMap;%0A
+import java.util.Map;%0A
 import j
@@ -134,16 +134,51 @@
 rayList;
+%0Aimport jp.co.flect.json.JsonUtils;
 %0A%0Apublic
@@ -882,10 +882,546 @@
 ors();%7D%0A
+%09%0A%09public String toJson() %7B%0A%09%09LinkedHashMap%3CString, Object%3E map = new LinkedHashMap%3CString, Object%3E();%0A%09%09map.put(%22success%22, isSuccess());%0A%09%09if (hasErrors()) %7B%0A%09%09%09Map%3CString, List%3CString%3E%3E msgMap = new LinkedHashMap%3CString, List%3CString%3E%3E();%0A%09%09%09for (Map.Entry%3CFormItem, List%3CString%3E%3E entry : this.errors.entrySet()) %7B%0A%09%09%09%09msgMap.put(entry.getKey().getName(), entry.getValue());%0A%09%09%09%7D%0A%09%09%09if (this.commonErrors.size() %3E 0) %7B%0A%09%09%09%09msgMap.put(%22#%22, this.commonErrors);%0A%09%09%09%7D%0A%09%09%09map.put(%22messages%22, msgMap);%0A%09%09%7D%0A%09%09return JsonUtils.toJson(map);%0A%09%7D%0A
 %7D%0A

Add ValidationResult#toJson