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.ValidationConstraint; |
24 |
| import org.apache.tapestry.valid.ValidatorException; |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| public class RequirableFieldSupportImpl implements RequirableFieldSupport |
34 |
| { |
35 |
| |
36 |
| |
37 |
| |
38 |
29
| public void render(RequirableField component, IMarkupWriter writer, IRequestCycle cycle)
|
39 |
| { |
40 |
29
| IForm form = component.getForm();
|
41 |
| |
42 |
29
| if (component.isRequired() && form.isClientValidationEnabled())
|
43 |
| { |
44 |
1
| String function = "require(event, document." + form.getName() + "." + component.getName() + ",'" + buildRequiredMessage(component) + "')";
|
45 |
| |
46 |
1
| form.addEventHandler(FormEventType.SUBMIT, function);
|
47 |
| } |
48 |
| } |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
19
| public void rewind(RequirableField component, IMarkupWriter writer, IRequestCycle cycle)
|
54 |
| { |
55 |
19
| try
|
56 |
| { |
57 |
19
| String value = component.getSubmittedValue(cycle);
|
58 |
| |
59 |
19
| if (component.isRequired() && HiveMind.isBlank(value))
|
60 |
| { |
61 |
1
| throw new ValidatorException(buildRequiredMessage(component), ValidationConstraint.REQUIRED);
|
62 |
| } |
63 |
| |
64 |
18
| component.bind(writer, cycle);
|
65 |
| } |
66 |
| catch (ValidatorException e) |
67 |
| { |
68 |
1
| component.getForm().getDelegate().record(e);
|
69 |
| } |
70 |
| } |
71 |
| |
72 |
2
| protected String buildRequiredMessage(RequirableField component)
|
73 |
| { |
74 |
2
| return MessageFormat.format(component.getRequiredMessage(), new Object[] { component.getDisplayName() });
|
75 |
| } |
76 |
| } |