Skip to content

Commit c6bed93

Browse files
committed
Spring MVC Form Validation - Validation a Number Range.
Updated code from Applying Build-In Validation Rules commit which was not restored afters testing.
1 parent 8755540 commit c6bed93

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

src/com/luv2code/springdemo/mvc/Customer.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.luv2code.springdemo.mvc;
22

33

4+
import javax.validation.constraints.Max;
5+
import javax.validation.constraints.Min;
46
import javax.validation.constraints.NotNull;
57
import javax.validation.constraints.Size;
68

@@ -9,9 +11,13 @@ public class Customer {
911
private String firstName;
1012

1113
@NotNull(message="is required")
12-
@Size(min=3, message="is required")
14+
@Size(min=1, message="is required")
1315
private String lastName;
1416

17+
@Min(value=0, message = "must be greater than or equal to zero")
18+
@Max(value=10, message = "must be less than or equal to 10")
19+
private int freePasses;
20+
1521
public String getFirstName() {
1622
return firstName;
1723
}
@@ -27,4 +33,12 @@ public String getLastName() {
2733
public void setLastName(String lastName) {
2834
this.lastName = lastName;
2935
}
36+
37+
public int getFreePasses() {
38+
return freePasses;
39+
}
40+
41+
public void setFreePasses(int freePasses) {
42+
this.freePasses = freePasses;
43+
}
3044
}

src/com/luv2code/springdemo/mvc/CustomerController.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ public String showForm(Model model) {
3535
@RequestMapping("/processForm")
3636
public String processForm(@Valid @ModelAttribute("customer") Customer customer,
3737
BindingResult bindingResult) {
38-
39-
System.out.println("Last name: |" + customer.getLastName() + "|" );
40-
4138
if (bindingResult.hasErrors()) {
4239
return "customer-form";
4340
} else {

web/WEB-INF/view/customer-confirmation.jsp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
<body>
1010

1111
The customer is confirmed: ${customer.firstName} ${customer.lastName}
12+
1213
<br><br>
1314

15+
Free passes: ${customer.freePasses}
16+
1417
</body>
1518

1619
</html>

web/WEB-INF/view/customer-form.jsp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525

2626
<br><br>
2727

28+
Free passes: <form:input path="freePasses" />
29+
<form:errors path="freePasses" cssClass="error" />
30+
31+
<br><br>
32+
2833
<input type="submit" value="Submit" />
2934

3035
</form:form>

0 commit comments

Comments
 (0)