Fix: Add validation to prevent creating a pet with an empty name

This commit is contained in:
Sagar_Sharma 2025-12-11 09:46:13 +05:30
parent 3e1ce239f4
commit dac434ef87

View file

@ -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");