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