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