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