mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-24 00:35:49 +00:00
add birthdate validation
This commit is contained in:
parent
b4efc934b2
commit
32d6b95e79
1 changed files with 11 additions and 0 deletions
|
@ -15,6 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.owner;
|
package org.springframework.samples.petclinic.owner;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
|
@ -87,6 +88,10 @@ class PetController {
|
||||||
if (StringUtils.hasLength(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null) {
|
if (StringUtils.hasLength(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null) {
|
||||||
result.rejectValue("name", "duplicate", "already exists");
|
result.rejectValue("name", "duplicate", "already exists");
|
||||||
}
|
}
|
||||||
|
LocalDate currentDate = LocalDate.now();
|
||||||
|
if (pet.getBirthDate() != null && pet.getBirthDate().isAfter(currentDate)) {
|
||||||
|
result.rejectValue("birthDate", "invalid", "Invalid birth date. Please select a valid date.");
|
||||||
|
}
|
||||||
|
|
||||||
owner.addPet(pet);
|
owner.addPet(pet);
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
|
@ -107,6 +112,12 @@ class PetController {
|
||||||
|
|
||||||
@PostMapping("/pets/{petId}/edit")
|
@PostMapping("/pets/{petId}/edit")
|
||||||
public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owner, ModelMap model) {
|
public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owner, ModelMap model) {
|
||||||
|
|
||||||
|
LocalDate currentDate = LocalDate.now();
|
||||||
|
if (pet.getBirthDate() != null && pet.getBirthDate().isAfter(currentDate)) {
|
||||||
|
result.rejectValue("birthDate", "invalid", "Invalid birth date. Please select a valid date.");
|
||||||
|
}
|
||||||
|
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
model.put("pet", pet);
|
model.put("pet", pet);
|
||||||
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
||||||
|
|
Loading…
Reference in a new issue