Skip to content

Commit c2a2bab

Browse files
sukhminderaroradsyer
authored andcommitted
fix: Add constant and improvement in PetClinic Signed-off-by: Sukhminder Arora <[email protected]>
Signed-off-by: Sukhminder Arora <[email protected]>
1 parent aa2273e commit c2a2bab

File tree

3 files changed

+28
-30
lines changed

3 files changed

+28
-30
lines changed

src/main/java/org/springframework/samples/petclinic/model/Person.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
public class Person extends BaseEntity {
2929

3030
@Column(name = "first_name")
31-
@NotBlank
31+
@NotBlank(message = "First name is required")
3232
private String firstName;
3333

3434
@Column(name = "last_name")
35-
@NotBlank
35+
@NotBlank(message = "Last name is required")
3636
private String lastName;
3737

3838
public String getFirstName() {

src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
class OwnerController {
4848

4949
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";
50+
public static final String REDIRECT_OWNERS = "redirect:/owners/";
5051

5152
private final OwnerRepository owners;
5253

@@ -81,7 +82,7 @@ public String processCreationForm(@Valid Owner owner, BindingResult result, Redi
8182

8283
this.owners.save(owner);
8384
redirectAttributes.addFlashAttribute("message", "New Owner Created");
84-
return "redirect:/owners/" + owner.getId();
85+
return REDIRECT_OWNERS + owner.getId();
8586
}
8687

8788
@GetMapping("/owners/find")
@@ -108,7 +109,7 @@ public String processFindForm(@RequestParam(defaultValue = "1") int page, Owner
108109
if (ownersResults.getTotalElements() == 1) {
109110
// 1 owner found
110111
owner = ownersResults.iterator().next();
111-
return "redirect:/owners/" + owner.getId();
112+
return REDIRECT_OWNERS + owner.getId();
112113
}
113114

114115
// multiple owners found
Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
<html>
2-
<body>
3-
<form>
4-
<th:block th:fragment="input (label, name, type)">
5-
<div th:with="valid=${!#fields.hasErrors(name)}"
6-
th:class="${'form-group' + (valid ? '' : ' has-error')}"
7-
class="form-group">
8-
<label class="col-sm-2 control-label" th:text="${label}">Label</label>
9-
<div class="col-sm-10">
10-
<div th:switch="${type}">
11-
<input th:case="'text'" class="form-control" type="text" th:field="*{__${name}__}" />
12-
<input th:case="'date'" class="form-control" type="date" th:field="*{__${name}__}"/>
13-
</div>
14-
<span th:if="${valid}"
15-
class="fa fa-ok form-control-feedback"
16-
aria-hidden="true"></span>
17-
<th:block th:if="${!valid}">
18-
<span
19-
class="fa fa-remove form-control-feedback"
20-
aria-hidden="true"></span>
21-
<span class="help-inline" th:errors="*{__${name}__}">Error</span>
22-
</th:block>
23-
</div>
1+
<html xmlns:th="http://www.thymeleaf.org">
2+
<!-- Define the fragment 'input' that takes label, name, and type as parameters -->
3+
<th:block th:fragment="input(label, name, type)">
4+
<div th:with="valid=${!#fields.hasErrors(name)}"
5+
th:class="'form-group' + (valid ? '' : ' has-error')">
6+
<label class="col-sm-2 control-label" th:text="${label}">Label</label>
7+
<div class="col-sm-10">
8+
<div th:switch="${type}">
9+
<!-- For text input -->
10+
<input th:case="'text'" class="form-control" type="text" th:field="*{__${name}__}" />
11+
<!-- For date input -->
12+
<input th:case="'date'" class="form-control" type="date" th:field="*{__${name}__}" />
13+
<!-- Add other input types as needed -->
2414
</div>
25-
</th:block>
26-
</form>
27-
</body>
15+
<!-- Show a check icon if there are no errors -->
16+
<span th:if="${valid}" class="fa fa-ok form-control-feedback" aria-hidden="true"></span>
17+
<!-- When there are errors, show an error icon and display error messages -->
18+
<th:block th:if="${!valid}">
19+
<span class="fa fa-remove form-control-feedback" aria-hidden="true"></span>
20+
<span class="help-inline" th:errors="*{__${name}__}">Error</span>
21+
</th:block>
22+
</div>
23+
</div>
24+
</th:block>
2825
</html>

0 commit comments

Comments
 (0)