1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.valid; |
16 |
| |
17 |
| import org.apache.tapestry.IMarkupWriter; |
18 |
| import org.apache.tapestry.IRequestCycle; |
19 |
| import org.apache.tapestry.Tapestry; |
20 |
| import org.apache.tapestry.form.AbstractFormComponent; |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| public abstract class ValidField extends AbstractFormComponent |
36 |
| { |
37 |
| public abstract boolean isHidden(); |
38 |
| |
39 |
| public abstract boolean isDisabled(); |
40 |
| |
41 |
| public abstract Object getValue(); |
42 |
| |
43 |
| public abstract void setValue(Object value); |
44 |
| |
45 |
| |
46 |
| |
47 |
| public abstract String getDisplayName(); |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
6
| protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
|
54 |
| { |
55 |
6
| IValidationDelegate delegate = getForm().getDelegate();
|
56 |
| |
57 |
6
| delegate.registerForFocus(this, delegate.isInError() ? ValidationConstants.ERROR_FIELD
|
58 |
| : ValidationConstants.NORMAL_FIELD); |
59 |
| |
60 |
6
| delegate.writePrefix(writer, cycle, this, null);
|
61 |
| |
62 |
6
| writer.beginEmpty("input");
|
63 |
| |
64 |
6
| writer.attribute("type", isHidden() ? "password" : "text");
|
65 |
| |
66 |
6
| if (isDisabled())
|
67 |
0
| writer.attribute("disabled", "disabled");
|
68 |
| |
69 |
6
| writer.attribute("name", getName());
|
70 |
| |
71 |
6
| String value = readValue();
|
72 |
6
| if (value != null)
|
73 |
4
| writer.attribute("value", value);
|
74 |
| |
75 |
6
| renderIdAttribute(writer, cycle);
|
76 |
| |
77 |
6
| renderInformalParameters(writer, cycle);
|
78 |
| |
79 |
6
| delegate.writeAttributes(writer, cycle, this, null);
|
80 |
| |
81 |
6
| IValidator validator = getValidator();
|
82 |
| |
83 |
6
| if (validator == null)
|
84 |
0
| throw Tapestry.createRequiredParameterException(this, "validator");
|
85 |
| |
86 |
6
| if (validator.isRequired())
|
87 |
2
| delegate.registerForFocus(this, ValidationConstants.REQUIRED_FIELD);
|
88 |
| |
89 |
6
| validator.renderValidatorContribution(this, writer, cycle);
|
90 |
| |
91 |
6
| writer.closeTag();
|
92 |
| |
93 |
6
| delegate.writeSuffix(writer, cycle, this, null);
|
94 |
| } |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
4
| protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
|
101 |
| { |
102 |
4
| String value = cycle.getParameter(getName());
|
103 |
| |
104 |
4
| updateValue(value);
|
105 |
| } |
106 |
| |
107 |
6
| protected String readValue()
|
108 |
| { |
109 |
6
| IValidator validator = getValidator();
|
110 |
6
| if (validator == null)
|
111 |
0
| throw Tapestry.createRequiredParameterException(this, "validator");
|
112 |
| |
113 |
6
| IValidationDelegate delegate = getForm().getDelegate();
|
114 |
| |
115 |
6
| if (delegate.isInError())
|
116 |
2
| return delegate.getFieldInputValue();
|
117 |
| |
118 |
4
| Object value = getValue();
|
119 |
| |
120 |
4
| String result = validator.toString(this, value);
|
121 |
| |
122 |
4
| return result;
|
123 |
| } |
124 |
| |
125 |
4
| protected void updateValue(String value)
|
126 |
| { |
127 |
4
| Object objectValue = null;
|
128 |
| |
129 |
4
| IValidator validator = getValidator();
|
130 |
4
| if (validator == null)
|
131 |
2
| throw Tapestry.createRequiredParameterException(this, "validator");
|
132 |
| |
133 |
2
| IValidationDelegate delegate = getForm().getDelegate();
|
134 |
| |
135 |
2
| delegate.recordFieldInputValue(value);
|
136 |
| |
137 |
2
| try
|
138 |
| { |
139 |
2
| objectValue = validator.toObject(this, value);
|
140 |
| } |
141 |
| catch (ValidatorException ex) |
142 |
| { |
143 |
0
| delegate.record(ex);
|
144 |
0
| return;
|
145 |
| } |
146 |
| |
147 |
2
| setValue(objectValue);
|
148 |
| } |
149 |
| |
150 |
| public abstract IValidator getValidator(); |
151 |
| } |