KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > FormErrors


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.forms;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.riotfamily.common.markup.DocumentWriter;
32 import org.riotfamily.common.markup.Html;
33 import org.riotfamily.forms.support.AbstractBindingResultSupport;
34 import org.springframework.beans.PropertyEditorRegistry;
35 import org.springframework.util.StringUtils;
36 import org.springframework.validation.FieldError;
37
38 public class FormErrors extends AbstractBindingResultSupport {
39
40     private static final String JavaDoc GENERAL_FORM_ERROR_MESSAGE_KEY =
41             "error.form.hasErrors";
42
43     private static final String JavaDoc GENERAL_FORM_ERROR_DEFAULT_MESSAGE =
44             "Please correct the error(s) below.";
45
46     private Form form;
47
48     public FormErrors(Form form) {
49         super(form.getId());
50         this.form = form;
51         setMessageCodesResolver(form.getFormContext().getMessageResolver()
52                 .getMessageCodesResolver());
53     }
54
55     public Object JavaDoc getTarget() {
56         return form.getBackingObject();
57     }
58
59     public Object JavaDoc getFieldValue(String JavaDoc field) {
60         return form.getEditor(field).getValue();
61     }
62
63     public void renderErrors(Element element) {
64         List JavaDoc errors = getErrors(element);
65         if (errors != null) {
66             PrintWriter JavaDoc writer = element.getForm().getFormContext().getWriter();
67             DocumentWriter tag = new DocumentWriter(writer);
68             tag.start(Html.UL)
69                     .attribute(Html.COMMON_ID, element.getId() + "-error")
70                     .attribute(Html.COMMON_CLASS, "errors");
71             Iterator JavaDoc it = errors.iterator();
72             while (it.hasNext()) {
73                 tag.start(Html.LI)
74                         .body((String JavaDoc) it.next())
75                         .end();
76             }
77             tag.end();
78         }
79     }
80
81     public List JavaDoc getErrors(Element element) {
82         if (element instanceof Editor) {
83             ArrayList JavaDoc messages = new ArrayList JavaDoc();
84             Editor editor = (Editor) element;
85             List JavaDoc fieldErrors = getFieldErrors(editor.getFieldName());
86             for (Iterator JavaDoc it = fieldErrors.iterator(); it.hasNext();) {
87                 FieldError error = (FieldError) it.next();
88                 String JavaDoc message = form.getFormContext().getMessageResolver().getMessage(error);
89                 if (!StringUtils.hasLength(message)) {
90                     message = StringUtils.arrayToCommaDelimitedString(error.getCodes());
91                 }
92                 messages.add(message);
93             }
94             return messages;
95         }
96         return null;
97     }
98
99     public void removeErrors(Element element) {
100         if (element instanceof Editor) {
101             Editor editor = (Editor) element;
102             removeErrors(getFieldErrors(editor.getFieldName()));
103         }
104     }
105
106     public String JavaDoc getGeneralFormError() {
107         return form.getFormContext().getMessageResolver().getMessage(
108                 GENERAL_FORM_ERROR_MESSAGE_KEY, null,
109                 GENERAL_FORM_ERROR_DEFAULT_MESSAGE);
110     }
111
112     public boolean hasErrors(Element element) {
113         if (!(element instanceof Editor)) {
114             return false;
115         }
116         Editor editor = (Editor) element;
117         return hasFieldErrors(editor.getFieldName());
118     }
119
120     public PropertyEditorRegistry getPropertyEditorRegistry() {
121         return form.getEditorBinder();
122     }
123
124 }
125
Popular Tags