US01, US03: Fix bug where all whitespaces were considered valid input on horse create and update

This commit is contained in:
Ivaylo Ivanov 2020-04-05 18:30:31 +02:00
parent a01405e3cc
commit 8e3736848d
4 changed files with 8 additions and 8 deletions

View File

@ -131,7 +131,7 @@ public class HorseJdbcDao implements HorseDao {
int changes = jdbcTemplate.update(connection -> { int changes = jdbcTemplate.update(connection -> {
PreparedStatement ps = connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS); PreparedStatement ps = connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
ps.setString(1, horse.getName()); ps.setString(1, horse.getName().trim());
ps.setString(2, horse.getDescription()); ps.setString(2, horse.getDescription());
ps.setInt(3, horse.getScore()); ps.setInt(3, horse.getScore());
ps.setDate(4, horse.getBirthday()); ps.setDate(4, horse.getBirthday());
@ -179,7 +179,7 @@ public class HorseJdbcDao implements HorseDao {
int changes = jdbcTemplate.update(connection -> { int changes = jdbcTemplate.update(connection -> {
PreparedStatement ps = connection.prepareStatement(sql); PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, horse.getName()); ps.setString(1, horse.getName().trim());
ps.setString(2, horse.getDescription()); ps.setString(2, horse.getDescription());
ps.setInt(3, horse.getScore()); ps.setInt(3, horse.getScore());
ps.setDate(4, horse.getBirthday()); ps.setDate(4, horse.getBirthday());

View File

@ -33,7 +33,7 @@ public class Validator {
} }
public void validateNewHorse(Horse horse) throws ValidationException { public void validateNewHorse(Horse horse) throws ValidationException {
if(horse.getName() == null || horse.getScore() == 0 || horse.getBirthday() == null || horse.getRace() == null || horse.getImagePath() == null){ if(horse.getName() == null || horse.getName().isEmpty() || horse.getName().isBlank() || horse.getScore() == 0 || horse.getBirthday() == null || horse.getRace() == null || horse.getImagePath() == null){
throw new ValidationException("All or some required values missing: name, score, birthday, race, imagePath"); throw new ValidationException("All or some required values missing: name, score, birthday, race, imagePath");
} }
if(horse.getScore() > 5 || horse.getScore() < 1) { if(horse.getScore() > 5 || horse.getScore() < 1) {
@ -48,7 +48,7 @@ public class Validator {
if(horse.getId() == null || horse.getId() == 0) { if(horse.getId() == null || horse.getId() == 0) {
throw new ValidationException("Horse Id cannot be null or 0"); throw new ValidationException("Horse Id cannot be null or 0");
} }
if(horse.getName() == null || horse.getScore() == 0 || horse.getBirthday() == null || horse.getRace() == null || horse.getImagePath() == null){ if(horse.getName() == null || horse.getName().isEmpty() || horse.getName().isBlank() || horse.getScore() == 0 || horse.getBirthday() == null || horse.getRace() == null || horse.getImagePath() == null){
throw new ValidationException("All or some required values missing: name, score, birthday, race. imagePath"); throw new ValidationException("All or some required values missing: name, score, birthday, race. imagePath");
} }
if(horse.getScore() > 5 || horse.getScore() < 1) { if(horse.getScore() > 5 || horse.getScore() < 1) {

View File

@ -17,12 +17,12 @@
<form (ngSubmit)="addHorse()" #horseForm="ngForm"> <form (ngSubmit)="addHorse()" #horseForm="ngForm">
<div class="form-group"> <div class="form-group">
<label for="name">Name</label> <label for="name">Name</label>
<input type="text" class="form-control" name="name" [(ngModel)]="horse.name" required> <input type="text" class="form-control" name="name" pattern="\S+.*" [(ngModel)]="horse.name" required>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="description">Description</label> <label for="description">Description</label>
<textarea class="form-control" name="description" [(ngModel)]="horse.description" value=""></textarea> <textarea class="form-control" name="description" pattern="\S+.*" [(ngModel)]="horse.description" value=""></textarea>
</div> </div>
<div class="form-group"> <div class="form-group">

View File

@ -17,12 +17,12 @@
<form (ngSubmit)="updateHorse()" #horseForm="ngForm"> <form (ngSubmit)="updateHorse()" #horseForm="ngForm">
<div class="form-group"> <div class="form-group">
<label for="name">Name</label> <label for="name">Name</label>
<input type="text" class="form-control" name="name" [(ngModel)]="horse.name" required> <input type="text" class="form-control" name="name" [(ngModel)]="horse.name" pattern="\S+.*" required>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="description">Description</label> <label for="description">Description</label>
<textarea class="form-control" name="description" [(ngModel)]="horse.description"></textarea> <textarea class="form-control" name="description" pattern="\S+.*" [(ngModel)]="horse.description"></textarea>
</div> </div>
<div class="form-group"> <div class="form-group">