This repository was archived by the owner on Jan 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathbean-validation002.html
More file actions
524 lines (502 loc) · 21.2 KB
/
Copy pathbean-validation002.html
File metadata and controls
524 lines (502 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Using Bean Validation Constraints</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="css/style.css" rel="stylesheet">
<script src="https://use.fontawesome.com/96c4d89611.js"></script>
</head>
<body>
<table id="doc-title" cellspacing="0" cellpadding="0">
<tr>
<td align="left" valign="top">
<b>Java Platform, Enterprise Edition (Java EE) 8</b><br />
<b>The Java EE Tutorial</b>
<!-- <p class="beta">Beta Draft (Pre-General Availability)</p> -->
</td>
</tr>
</table>
<hr />
<table width="90%" id="top-nav" cellspacing="0" cellpadding="0">
<colgroup>
<col width="12%"/>
<col width="12%"/>
<col width="*"/>
</colgroup>
<tr>
<td align="left">
<a href="bean-validation001.html">
<span class="vector-font"><i class="fa fa-arrow-circle-left" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Previous</span>
</a>
</td>
<td align="left">
<a href="bean-validation003.html">
<span class=" vector-font"><i class="fa fa-arrow-circle-right vector-font" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Next</span>
</a>
</td>
<td align="right">
<a href="toc.html">
<span class=" vector-font"><i class="fa fa-list vector-font" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Contents</span>
</a>
</td>
</tr>
</table>
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p><a id="GIRCZ"></a><a id="using-bean-validation-constraints"></a></p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_using_bean_validation_constraints">Using Bean Validation Constraints</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The Bean Validation model is supported by constraints in the form of
annotations placed on a field, method, or class of a JavaBeans
component, such as a managed bean.</p>
</div>
<div class="paragraph">
<p>Constraints can be built in or user defined. User-defined constraints
are called custom constraints. Several built-in constraints are
available in the <code>javax.validation.constraints</code> package.
<a href="#GKAGK">Table 23-1</a> lists all the built-in constraints. See
<a href="bean-validation-advanced001.html#GKFGX">Creating Custom Constraints</a>
for information on creating custom constraints.</p>
</div>
<div class="paragraph">
<p><a id="sthref121"></a><a id="GKAGK"></a></p>
</div>
<div class="paragraph">
<p>Table 23-1 Built-In Bean Validation Constraints</p>
</div>
<table class="tableblock frame-all grid-all" style="width: 99%;">
<colgroup>
<col style="width: 22.2222%;">
<col style="width: 59.5959%;">
<col style="width: 18.1819%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Constraint</strong></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Description</strong></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Example</strong></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@AssertFalse</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The value of the field or property must be <code>false</code>.</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@AssertFalse
boolean isUnsupported;</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@AssertTrue</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The value of the field or property must be <code>true</code>.</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@AssertTrue
boolean isActive;</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@DecimalMax</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The value of the field or property must be a decimal
value lower than or equal to the number in the value element.</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@DecimalMax("30.00")
BigDecimal discount;</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@DecimalMin</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The value of the field or property must be a decimal
value greater than or equal to the number in the value element.</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@DecimalMin("5.00")
BigDecimal discount;</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Digits</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The value of the field or property must be a number within a
specified range. The <code>integer</code> element specifies the maximum integral
digits for the number, and the <code>fraction</code> element specifies the maximum
fractional digits for the number.</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@Digits(integer=6, fraction=2)
BigDecimal price;</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Email</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The value of the field or property must be a valid email address.</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@Email
String emailaddress;</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Future</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The value of the field or property must be a date in the
future.</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@Future
Date eventDate;</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@FutureOrPresent</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">TThe value of the field or property must be a date or time in present or future.</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@FutureOrPresent
Time travelTime;</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Max</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The value of the field or property must be an integer value
lower than or equal to the number in the value element.</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@Max(10)
int quantity;</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Min</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The value of the field or property must be an integer value
greater than or equal to the number in the value element.</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@Min(5)
int quantity;</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Negative</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The value of the field or property must be a negative number.</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@Negative
int basementFloor;</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@NegativeOrZero</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The value of the field or property must be negative or zero.</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@NegativeOrZero
int debtValue;</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@NotBlank</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The value of the field or property must contain atleast one non-white space character.</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@NotBlank
String message;</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@NotEmpty</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The value of the field or property must not be empty. The length of the characters or array, and the size of a collection or map are evaluated.</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@NotEmpty
String message;;</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@NotNull</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The value of the field or property must not be null.</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@NotNull
String username;</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Null</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The value of the field or property must be null.</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@Null
String unusedString;</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Past</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The value of the field or property must be a date in the past.</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@Past
Date birthday;</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@PastOrPresent</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The value of the field or property must be a date or time in the past or present.</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@PastOrPresent
Date travelDate;</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Pattern</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The value of the field or property must match the regular
expression defined in the <code>regexp</code> element.</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@Pattern(regexp="\\(\\d{3}\\)\\d{3}-\\d{4}")
String phoneNumber;</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Positive</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The value of the field or property must be a positive number.</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@Positive
BigDecimal area;</code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@PositiveOrZero</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The value of the field or property must be a positive number or zero. .</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@PositiveOrZero
int totalGoals;</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Size</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The size of the field or property is evaluated and must match
the specified boundaries. If the field or property is a <code>String</code>, the
size of the string is evaluated. If the field or property is a
<code>Collection</code>, the size of the <code>Collection</code> is evaluated. If the field or
property is a <code>Map</code>, the size of the <code>Map</code> is evaluated. If the field or
property is an array, the size of the array is evaluated. Use one of the
optional <code>max</code> or <code>min</code> elements to specify the boundaries.</p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@Size(min=2, max=240)
String briefMessage;</code></pre>
</div>
</div></div></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>In the following example, a constraint is placed on a field using the
built-in <code>@NotNull</code> constraint:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">public class Name {
@NotNull
private String firstname;
@NotNull
private String lastname;
...
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>You can also place more than one constraint on a single JavaBeans
component object. For example, you can place an additional constraint
for size of field on the <code>firstname</code> and the <code>lastname</code> fields:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">public class Name {
@NotNull
@Size(min=1, max=16)
private String firstname;
@NotNull
@Size(min=1, max=16)
private String lastname;
...
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The following example shows a method with a user-defined constraint that
checks user-defined constraint that checks for a predefined phone number pattern, such as a country specific phone number:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@USPhoneNumber
public String getPhone() {
return phone;
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>For a built-in constraint, a default implementation is available. A
user-defined or custom constraint needs a validation implementation. In
the preceding example, the <code>@USPhoneNumber</code> custom constraint needs an
implementation class.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_repeating_annotations">Repeating Annotations</h2>
<div class="sectionbody">
<div id="repeating_annotations" class="paragraph">
<p>In Bean Validation 2.0, you can specify the same constraint several times on a validation target using repeating annotation:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">public class Account {
@Max (value = 2000, groups = Default.class, message = "max.value")
@Max (value = 5000, groups = GoldCustomer.class, message = "max.value")
private long withdrawalAmount;
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>All in-built constraints from <code>javax .validation.constraints</code> package support repeatable annotations. Similarly, custom constraints can use <code>@Repeatable</code> annotation. In the following sample, depending on whether the group is <code>PeakHour</code> or <code>NonPeakHour</code>, the car instance is validated as either two passengers or three passengers based car, and then listed as eligible in the car pool lane:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">/**
* Validate whether a car is eligible for car pool lane
*/
@Documented
@Constraint(validatedBy = CarPoolValidator.class)
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
@Repeatable(List.class)
public @interface CarPool {
String message() default "{CarPool.message}";
Class<?>[] groups() default {};
int value();
Class<? extends Payload>[] payload() default {};
/**
* Defines several @CarPool annotations on the same element
* @see (@link CarPool}
*/
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
@Documented
@interface List {
CarPool[] value();
}
}
public class Car{
private String registrationNumber;
@CarPool(value = 2, group = NonPeakHour.class)
@CarPool(value = 3, group = {Default.class, PeakHour.class})
private int totalPassengers;
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Any validation failures are gracefully handled and can be displayed by
the <code>h:messages</code> tag.</p>
</div>
<div class="paragraph">
<p>Any managed bean that contains Bean Validation annotations automatically
gets validation constraints placed on the fields on a JavaServer Faces
application’s web pages.</p>
</div>
<div class="paragraph">
<p>For more information on using validation constraints, see the following:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="bean-validation-advanced.html#GKAHP">Chapter 24, "Bean Validation:
Advanced Topics"</a></p>
</li>
<li>
<p><a href="jaxrs-advanced002.html#BABCJEDF">Validating Resource Data with Bean
Validation</a></p>
</li>
<li>
<p><a href="persistence-intro002.html#GKAHQ">Validating Persistent Fields and
Properties</a></p>
</li>
</ul>
</div>
</div>
</div>
<hr />
<table width="90%" id="bottom-nav" cellspacing="0" cellpadding="0">
<colgroup>
<col width="12%"/>
<col width="12%"/>
<col width="*"/>
</colgroup>
<tr>
<td align="left">
<a href="bean-validation001.html">
<span class=" vector-font"><i class="fa fa-arrow-circle-left" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Previous</span>
</a>
</td>
<td align="left">
<a href="bean-validation003.html">
<span class="vector-font"><i class="fa fa-arrow-circle-right vector-font" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Next</span>
</a>
</td>
<td align="right">
<a href="toc.html">
<span class="vector-font"><i class="fa fa-list vector-font" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Contents</span>
</a>
</td>
</tr>
</table>
<span id="copyright">
<a href="img/cpyr.adoc">
<img src="img/oracle.gif" height="10px" alt="Oracle Logo" />
<span>Copyright © 2017, Oracle and/or its affiliates. All rights reserved.</span>
</a>
</span>
<!--<p align="center" class="beta">Beta Draft (Pre-General Availability)</p> -->
</body>
</html>