mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-12-27 19:07:28 +00:00
Fix: Add validation to prevent creating a pet with an empty name
This commit is contained in:
parent
3e1ce239f4
commit
dac434ef87
1 changed files with 5 additions and 0 deletions
|
|
@ -105,6 +105,11 @@ class PetController {
|
|||
@PostMapping("/pets/new")
|
||||
public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result,
|
||||
RedirectAttributes redirectAttributes) {
|
||||
// Validate empty name
|
||||
if (!StringUtils.hasText(pet.getName())) {
|
||||
result.rejectValue("name", "required", "Pet name must not be empty");
|
||||
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
|
||||
if (StringUtils.hasText(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null)
|
||||
result.rejectValue("name", "duplicate", "already exists");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue