File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\ui-samples-plugin\src\main\java\jenkins\plugins\ui_samples\FormFieldValidationWithContext.java.html

1 package jenkins.plugins.ui_samples;
2
3 import hudson.Extension;
4 import hudson.RelativePath;
5 import hudson.model.AbstractDescribableImpl;
6 import hudson.model.Descriptor;
7 import hudson.util.FormValidation;
8 import org.kohsuke.stapler.DataBoundConstructor;
9 import org.kohsuke.stapler.QueryParameter;
10
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.List;
14
15 /**
16 * How to access values of the nearby input fields when you do form field validation.
17 *
18 * @author Kohsuke Kawaguchi
19 */
20 @Extension
21 public class FormFieldValidationWithContext extends UISample {
22 private List<State> states = new ArrayList<State>(Arrays.asList(
23 new State("California",new City("Sacramento"), Arrays.asList(new City("San Francisco"),new City("Los Angeles"))),
24 new State("New York",new City("New York"), Arrays.asList(new City("Albany"),new City("Ithaca")))
25 ));
26
27 public FormFieldValidationWithContext() {
28 }
29
30 @DataBoundConstructor
31 public FormFieldValidationWithContext(List<State> states) {
32 this.states = states;
33 }
34
35 @Override
36 public String getDescription() {
37 return "How to access values of the nearby input fields when you do form field validation";
38 }
39
40 public List<State> getStates() {
41 return states;
42 }
43
44 @Override
45 public List<SourceFile> getSourceFiles() {
46 List<SourceFile> r = super.getSourceFiles();
47 r.add(new SourceFile("City/config.groovy"));
48 r.add(new SourceFile("State/config.groovy"));
49 return r;
50 }
51
52 public static class State extends AbstractDescribableImpl<State> {
53 /*
54 I'm lazy and just exposing fields as opposed to getter/setter.
55 Jenkins doesn't care and works correctly either way.
56 */
57 public String name;
58 public City capital;
59 public List<City> cities;
60
61 @DataBoundConstructor
62 public State(String name, City capital, List<City> cities) {
63 this.name = name;
64 this.capital = capital;
65 this.cities = cities;
66 }
67
68 @Extension
69 public static class DescriptorImpl extends Descriptor<State> {
70 @Override
71 public String getDisplayName() {
72 return "";
73 }
74
75 public FormValidation doCheckName(@QueryParameter String value,
76 @RelativePath("capital") @QueryParameter String name) {
77 /*
78 @RelativePath("capital") @QueryParameter
79 ... is short for
80 @RelativePath("capital") @QueryParameter("name")
81 ... and thus can be thought of "capital/name"
82
83 so this matches the current city name entered as the capital of this state
84 */
85
86 return FormValidation.ok("Are you sure " + name + " is a capital of " + value + "?");
87 }
88 }
89 }
90
91 public static class City extends AbstractDescribableImpl<City> {
92 public String name;
93
94 @DataBoundConstructor
95 public City(String name) {
96 this.name = name;
97 }
98
99 @Extension
100 public static class DescriptorImpl extends Descriptor<City> {
101 @Override
102 public String getDisplayName() {
103 return "";
104 }
105
106 public FormValidation doCheckName(@QueryParameter String value,
107 @RelativePath("..") @QueryParameter String name) {
108 /*
109 @RelativePath("..") @QueryParameter
110 ... is short for
111 @RelativePath("..") @QueryParameter("name")
112 ... and thus can be thought of "../name"
113
114 in the UI, fields for city is wrapped inside those of state, so "../name" binds
115 to the name field in the state.
116 */
117
118 if (name==null || value==null || value.contains(name)) return FormValidation.ok();
119 return FormValidation.warning("City name doesn't contain "+name);
120 }
121 }
122 }
123
124 @Extension
125 public static class DescriptorImpl extends UISampleDescriptor {
126 }
127 }
128