Skip to content

Commit

Permalink
adding pet name validation on edit Pet
Browse files Browse the repository at this point in the history
  • Loading branch information
bijomutta authored and dsyer committed Jul 12, 2023
1 parent 3be2895 commit 734ec77
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,9 @@ public Pet getPet(Integer id) {
public Pet getPet(String name, boolean ignoreNew) {
name = name.toLowerCase();
for (Pet pet : getPets()) {
if (!ignoreNew || !pet.isNew()) {
String compName = pet.getName();
compName = compName == null ? "" : compName.toLowerCase();
if (compName.equals(name)) {
String compName = pet.getName();
if (compName != null && compName.equalsIgnoreCase(name)) {
if (!ignoreNew || !pet.isNew()) {
return pet;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ public String initUpdateForm(Owner owner, @PathVariable("petId") int petId, Mode
@PostMapping("/pets/{petId}/edit")
public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owner, ModelMap model) {

String petName = pet.getName();

//checking if the pet name already exist for the owner
if (StringUtils.hasLength(petName)) {
Pet existingPet = owner.getPet(petName.toLowerCase(), false);
//adding error if pet name already exist
if (existingPet != null && existingPet.getId() != pet.getId()) {
result.rejectValue("name", "duplicate", "already exists");
}
}

LocalDate currentDate = LocalDate.now();
if (pet.getBirthDate() != null && pet.getBirthDate().isAfter(currentDate)) {
result.rejectValue("birthDate", "typeMismatch.birthDate");
Expand Down

0 comments on commit 734ec77

Please sign in to comment.