mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-15 12: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.Column;
|
||||||
import jakarta.persistence.MappedSuperclass;
|
import jakarta.persistence.MappedSuperclass;
|
||||||
import jakarta.validation.constraints.NotEmpty;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple JavaBean domain object representing an person.
|
* Simple JavaBean domain object representing an person.
|
||||||
|
@ -28,11 +28,11 @@ import jakarta.validation.constraints.NotEmpty;
|
||||||
public class Person extends BaseEntity {
|
public class Person extends BaseEntity {
|
||||||
|
|
||||||
@Column(name = "first_name")
|
@Column(name = "first_name")
|
||||||
@NotEmpty
|
@NotBlank
|
||||||
private String firstName;
|
private String firstName;
|
||||||
|
|
||||||
@Column(name = "last_name")
|
@Column(name = "last_name")
|
||||||
@NotEmpty
|
@NotBlank
|
||||||
private String lastName;
|
private String lastName;
|
||||||
|
|
||||||
public String getFirstName() {
|
public String getFirstName() {
|
||||||
|
|
|
@ -31,7 +31,7 @@ import jakarta.persistence.OneToMany;
|
||||||
import jakarta.persistence.OrderBy;
|
import jakarta.persistence.OrderBy;
|
||||||
import jakarta.persistence.Table;
|
import jakarta.persistence.Table;
|
||||||
import jakarta.validation.constraints.Digits;
|
import jakarta.validation.constraints.Digits;
|
||||||
import jakarta.validation.constraints.NotEmpty;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple JavaBean domain object representing an owner.
|
* Simple JavaBean domain object representing an owner.
|
||||||
|
@ -47,15 +47,15 @@ import jakarta.validation.constraints.NotEmpty;
|
||||||
public class Owner extends Person {
|
public class Owner extends Person {
|
||||||
|
|
||||||
@Column(name = "address")
|
@Column(name = "address")
|
||||||
@NotEmpty
|
@NotBlank
|
||||||
private String address;
|
private String address;
|
||||||
|
|
||||||
@Column(name = "city")
|
@Column(name = "city")
|
||||||
@NotEmpty
|
@NotBlank
|
||||||
private String city;
|
private String city;
|
||||||
|
|
||||||
@Column(name = "telephone")
|
@Column(name = "telephone")
|
||||||
@NotEmpty
|
@NotBlank
|
||||||
@Digits(fraction = 0, integer = 10)
|
@Digits(fraction = 0, integer = 10)
|
||||||
private String telephone;
|
private String telephone;
|
||||||
|
|
||||||
|
|
|
@ -95,7 +95,7 @@ class PetController {
|
||||||
|
|
||||||
@PostMapping("/pets/new")
|
@PostMapping("/pets/new")
|
||||||
public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result, ModelMap model) {
|
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");
|
result.rejectValue("name", "duplicate", "already exists");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,7 +127,7 @@ class PetController {
|
||||||
String petName = pet.getName();
|
String petName = pet.getName();
|
||||||
|
|
||||||
// checking if the pet name already exist for the owner
|
// 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);
|
Pet existingPet = owner.getPet(petName.toLowerCase(), false);
|
||||||
if (existingPet != null && existingPet.getId() != pet.getId()) {
|
if (existingPet != null && existingPet.getId() != pet.getId()) {
|
||||||
result.rejectValue("name", "duplicate", "already exists");
|
result.rejectValue("name", "duplicate", "already exists");
|
||||||
|
|
|
@ -38,7 +38,7 @@ public class PetValidator implements Validator {
|
||||||
Pet pet = (Pet) obj;
|
Pet pet = (Pet) obj;
|
||||||
String name = pet.getName();
|
String name = pet.getName();
|
||||||
// name validation
|
// name validation
|
||||||
if (!StringUtils.hasLength(name)) {
|
if (!StringUtils.hasText(name)) {
|
||||||
errors.rejectValue("name", REQUIRED, REQUIRED);
|
errors.rejectValue("name", REQUIRED, REQUIRED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ import org.springframework.samples.petclinic.model.BaseEntity;
|
||||||
import jakarta.persistence.Column;
|
import jakarta.persistence.Column;
|
||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.Entity;
|
||||||
import jakarta.persistence.Table;
|
import jakarta.persistence.Table;
|
||||||
import jakarta.validation.constraints.NotEmpty;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple JavaBean domain object representing a visit.
|
* Simple JavaBean domain object representing a visit.
|
||||||
|
@ -39,7 +39,7 @@ public class Visit extends BaseEntity {
|
||||||
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||||
private LocalDate date;
|
private LocalDate date;
|
||||||
|
|
||||||
@NotEmpty
|
@NotBlank
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -54,7 +54,7 @@ class ValidatorTests {
|
||||||
assertThat(constraintViolations).hasSize(1);
|
assertThat(constraintViolations).hasSize(1);
|
||||||
ConstraintViolation<Person> violation = constraintViolations.iterator().next();
|
ConstraintViolation<Person> violation = constraintViolations.iterator().next();
|
||||||
assertThat(violation.getPropertyPath().toString()).isEqualTo("firstName");
|
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