1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.form; |
16 |
| |
17 |
| import java.text.MessageFormat; |
18 |
| |
19 |
| import org.apache.hivemind.HiveMind; |
20 |
| import org.apache.tapestry.IForm; |
21 |
| import org.apache.tapestry.IMarkupWriter; |
22 |
| import org.apache.tapestry.IRequestCycle; |
23 |
| import org.apache.tapestry.valid.ValidationConstants; |
24 |
| import org.apache.tapestry.valid.ValidationConstraint; |
25 |
| import org.apache.tapestry.valid.ValidatorException; |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| public class RequirableFieldSupportImpl implements RequirableFieldSupport |
35 |
| { |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
29
| public void render(RequirableField component, IMarkupWriter writer, IRequestCycle cycle)
|
41 |
| { |
42 |
29
| IForm form = component.getForm();
|
43 |
| |
44 |
29
| if (component.isRequired())
|
45 |
| { |
46 |
2
| form.registerForFocus(component, ValidationConstants.REQUIRED_FIELD);
|
47 |
| |
48 |
2
| if (form.isClientValidationEnabled())
|
49 |
| { |
50 |
1
| String function = "require(event, document." + form.getName() + "."
|
51 |
| + component.getName() + ",'" + buildRequiredMessage(component) + "')"; |
52 |
| |
53 |
1
| form.addEventHandler(FormEventType.SUBMIT, function);
|
54 |
| } |
55 |
| } |
56 |
| } |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
19
| public void rewind(RequirableField component, IMarkupWriter writer, IRequestCycle cycle)
|
63 |
| { |
64 |
19
| try
|
65 |
| { |
66 |
19
| String value = component.getSubmittedValue(cycle);
|
67 |
| |
68 |
19
| if (component.isRequired() && HiveMind.isBlank(value))
|
69 |
| { |
70 |
1
| throw new ValidatorException(buildRequiredMessage(component),
|
71 |
| ValidationConstraint.REQUIRED); |
72 |
| } |
73 |
| |
74 |
18
| component.bind(writer, cycle);
|
75 |
| } |
76 |
| catch (ValidatorException e) |
77 |
| { |
78 |
1
| component.getForm().getDelegate().record(e);
|
79 |
| } |
80 |
| } |
81 |
| |
82 |
2
| protected String buildRequiredMessage(RequirableField component)
|
83 |
| { |
84 |
2
| return MessageFormat.format(component.getRequiredMessage(), new Object[]
|
85 |
| { component.getDisplayName() }); |
86 |
| } |
87 |
| } |