mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-15 20:25:50 +00:00
Change validation annotations and whitespace handling
This commit is contained in:
parent
0a529015bc
commit
4926e29270
6 changed files with 13 additions and 13 deletions
|
@ -17,7 +17,7 @@ package org.springframework.samples.petclinic.model;
|
|||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
/**
|
||||
* Simple JavaBean domain object representing an person.
|
||||
|
@ -28,11 +28,11 @@ import jakarta.validation.constraints.NotEmpty;
|
|||
public class Person extends BaseEntity {
|
||||
|
||||
@Column(name = "first_name")
|
||||
@NotEmpty
|
||||
@NotBlank
|
||||
private String firstName;
|
||||
|
||||
@Column(name = "last_name")
|
||||
@NotEmpty
|
||||
@NotBlank
|
||||
private String lastName;
|
||||
|
||||
public String getFirstName() {
|
||||
|
|
|
@ -31,7 +31,7 @@ import jakarta.persistence.OneToMany;
|
|||
import jakarta.persistence.OrderBy;
|
||||
import jakarta.persistence.Table;
|
||||
import jakarta.validation.constraints.Digits;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
/**
|
||||
* Simple JavaBean domain object representing an owner.
|
||||
|
@ -47,15 +47,15 @@ import jakarta.validation.constraints.NotEmpty;
|
|||
public class Owner extends Person {
|
||||
|
||||
@Column(name = "address")
|
||||
@NotEmpty
|
||||
@NotBlank
|
||||
private String address;
|
||||
|
||||
@Column(name = "city")
|
||||
@NotEmpty
|
||||
@NotBlank
|
||||
private String city;
|
||||
|
||||
@Column(name = "telephone")
|
||||
@NotEmpty
|
||||
@NotBlank
|
||||
@Digits(fraction = 0, integer = 10)
|
||||
private String telephone;
|
||||
|
||||
|
|
|
@ -95,7 +95,7 @@ class PetController {
|
|||
|
||||
@PostMapping("/pets/new")
|
||||
public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result, ModelMap model) {
|
||||
if (StringUtils.hasLength(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null) {
|
||||
if (StringUtils.hasText(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null) {
|
||||
result.rejectValue("name", "duplicate", "already exists");
|
||||
}
|
||||
|
||||
|
@ -127,7 +127,7 @@ class PetController {
|
|||
String petName = pet.getName();
|
||||
|
||||
// checking if the pet name already exist for the owner
|
||||
if (StringUtils.hasLength(petName)) {
|
||||
if (StringUtils.hasText(petName)) {
|
||||
Pet existingPet = owner.getPet(petName.toLowerCase(), false);
|
||||
if (existingPet != null && existingPet.getId() != pet.getId()) {
|
||||
result.rejectValue("name", "duplicate", "already exists");
|
||||
|
|
|
@ -38,7 +38,7 @@ public class PetValidator implements Validator {
|
|||
Pet pet = (Pet) obj;
|
||||
String name = pet.getName();
|
||||
// name validation
|
||||
if (!StringUtils.hasLength(name)) {
|
||||
if (!StringUtils.hasText(name)) {
|
||||
errors.rejectValue("name", REQUIRED, REQUIRED);
|
||||
}
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ import org.springframework.samples.petclinic.model.BaseEntity;
|
|||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Table;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
/**
|
||||
* Simple JavaBean domain object representing a visit.
|
||||
|
@ -39,7 +39,7 @@ public class Visit extends BaseEntity {
|
|||
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||
private LocalDate date;
|
||||
|
||||
@NotEmpty
|
||||
@NotBlank
|
||||
private String description;
|
||||
|
||||
/**
|
||||
|
|
|
@ -54,7 +54,7 @@ class ValidatorTests {
|
|||
assertThat(constraintViolations).hasSize(1);
|
||||
ConstraintViolation<Person> violation = constraintViolations.iterator().next();
|
||||
assertThat(violation.getPropertyPath().toString()).isEqualTo("firstName");
|
||||
assertThat(violation.getMessage()).isEqualTo("must not be empty");
|
||||
assertThat(violation.getMessage()).isEqualTo("must not be blank");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue