mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-04-24 11:22:48 +00:00
fix: Add constant and improvement in PetClinic Signed-off-by: Sukhminder Arora <sukhminderarora@gmail.com>
Signed-off-by: Sukhminder Arora <sukhminderarora@gmail.com>
This commit is contained in:
parent
aa2273e955
commit
c2a2bab3f6
3 changed files with 28 additions and 30 deletions
|
@ -28,11 +28,11 @@ import jakarta.validation.constraints.NotBlank;
|
|||
public class Person extends BaseEntity {
|
||||
|
||||
@Column(name = "first_name")
|
||||
@NotBlank
|
||||
@NotBlank(message = "First name is required")
|
||||
private String firstName;
|
||||
|
||||
@Column(name = "last_name")
|
||||
@NotBlank
|
||||
@NotBlank(message = "Last name is required")
|
||||
private String lastName;
|
||||
|
||||
public String getFirstName() {
|
||||
|
|
|
@ -47,6 +47,7 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
|||
class OwnerController {
|
||||
|
||||
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";
|
||||
public static final String REDIRECT_OWNERS = "redirect:/owners/";
|
||||
|
||||
private final OwnerRepository owners;
|
||||
|
||||
|
@ -81,7 +82,7 @@ class OwnerController {
|
|||
|
||||
this.owners.save(owner);
|
||||
redirectAttributes.addFlashAttribute("message", "New Owner Created");
|
||||
return "redirect:/owners/" + owner.getId();
|
||||
return REDIRECT_OWNERS + owner.getId();
|
||||
}
|
||||
|
||||
@GetMapping("/owners/find")
|
||||
|
@ -108,7 +109,7 @@ class OwnerController {
|
|||
if (ownersResults.getTotalElements() == 1) {
|
||||
// 1 owner found
|
||||
owner = ownersResults.iterator().next();
|
||||
return "redirect:/owners/" + owner.getId();
|
||||
return REDIRECT_OWNERS + owner.getId();
|
||||
}
|
||||
|
||||
// multiple owners found
|
||||
|
|
|
@ -1,28 +1,25 @@
|
|||
<html>
|
||||
<body>
|
||||
<form>
|
||||
<th:block th:fragment="input (label, name, type)">
|
||||
<div th:with="valid=${!#fields.hasErrors(name)}"
|
||||
th:class="${'form-group' + (valid ? '' : ' has-error')}"
|
||||
class="form-group">
|
||||
<label class="col-sm-2 control-label" th:text="${label}">Label</label>
|
||||
<div class="col-sm-10">
|
||||
<div th:switch="${type}">
|
||||
<input th:case="'text'" class="form-control" type="text" th:field="*{__${name}__}" />
|
||||
<input th:case="'date'" class="form-control" type="date" th:field="*{__${name}__}"/>
|
||||
</div>
|
||||
<span th:if="${valid}"
|
||||
class="fa fa-ok form-control-feedback"
|
||||
aria-hidden="true"></span>
|
||||
<th:block th:if="${!valid}">
|
||||
<span
|
||||
class="fa fa-remove form-control-feedback"
|
||||
aria-hidden="true"></span>
|
||||
<span class="help-inline" th:errors="*{__${name}__}">Error</span>
|
||||
</th:block>
|
||||
</div>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<!-- Define the fragment 'input' that takes label, name, and type as parameters -->
|
||||
<th:block th:fragment="input(label, name, type)">
|
||||
<div th:with="valid=${!#fields.hasErrors(name)}"
|
||||
th:class="'form-group' + (valid ? '' : ' has-error')">
|
||||
<label class="col-sm-2 control-label" th:text="${label}">Label</label>
|
||||
<div class="col-sm-10">
|
||||
<div th:switch="${type}">
|
||||
<!-- For text input -->
|
||||
<input th:case="'text'" class="form-control" type="text" th:field="*{__${name}__}" />
|
||||
<!-- For date input -->
|
||||
<input th:case="'date'" class="form-control" type="date" th:field="*{__${name}__}" />
|
||||
<!-- Add other input types as needed -->
|
||||
</div>
|
||||
</th:block>
|
||||
</form>
|
||||
</body>
|
||||
<!-- Show a check icon if there are no errors -->
|
||||
<span th:if="${valid}" class="fa fa-ok form-control-feedback" aria-hidden="true"></span>
|
||||
<!-- When there are errors, show an error icon and display error messages -->
|
||||
<th:block th:if="${!valid}">
|
||||
<span class="fa fa-remove form-control-feedback" aria-hidden="true"></span>
|
||||
<span class="help-inline" th:errors="*{__${name}__}">Error</span>
|
||||
</th:block>
|
||||
</div>
|
||||
</div>
|
||||
</th:block>
|
||||
</html>
|
||||
|
|
Loading…
Reference in a new issue