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