1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.nordija.tapestry.bayeux.delegate;
17
18 import org.apache.tapestry.IBinding;
19 import org.apache.tapestry.IMarkupWriter;
20 import org.apache.tapestry.IRequestCycle;
21 import org.apache.tapestry.form.IFormComponent;
22 import org.apache.tapestry.form.validator.Required;
23 import org.apache.tapestry.valid.IValidator;
24 import org.apache.tapestry.valid.ValidationDelegate;
25
26 import java.util.Collection;
27 import java.util.Map;
28
29
30
31
32
33
34
35
36
37
38
39 public class RequiredValidationDelegate extends ValidationDelegate {
40
41 public void writeSuffix(IMarkupWriter writer, IRequestCycle cycle, IFormComponent component, IValidator validator) {
42 if (isInError()) {
43 writer.printRaw(" ");
44 writer.begin("span");
45 writer.attribute("class", "error");
46 writer.print("!");
47 writer.end();
48 } else if (isRequired(component)) {
49 writer.printRaw(" ");
50 writer.begin("span");
51 writer.attribute("class", "required");
52 writer.print("*");
53 writer.end();
54 }
55
56 }
57
58
59
60
61
62
63
64
65 private boolean isRequired(IFormComponent component) {
66 Map bindings = component.getBindings();
67 IBinding validationBinding = (IBinding) bindings.get("validators");
68 boolean required = false;
69 if (validationBinding != null) {
70 Object validationBindingObject = validationBinding.getObject();
71 if (validationBindingObject != null && validationBindingObject instanceof Collection) {
72 Collection validators = (Collection) validationBindingObject;
73 for (java.util.Iterator iterator = validators.iterator(); iterator.hasNext();) {
74 Object aValidator = iterator.next();
75 if (aValidator instanceof Required) {
76 required = true;
77 break;
78 }
79 }
80 }
81 }
82 return required;
83 }
84 }