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