Reputation: 1
Here are my 2 domain classes:
package tacos;
import java.util.List;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import lombok.Data;
@Data
public class Taco {
@NotNull @Size(min=5, message="Name must be at least 5 characters long")
private String name;
@NotNull @Size(min=1, message="You must choose at least 1 ingredient")
private List\<Ingredient\> ingredients;
}
package tacos;
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.CreditCardNumber;
import java.util.List;
import java.util.ArrayList;
import lombok.Data;
@Data
public class TacoOrder {
@NotBlank(message = "Delivery name is required")
private String deliveryName;
@NotBlank(message = "Street is required")
private String deliveryStreet;
@NotBlank(message = "City is required")
private String deliveryCity;
@NotBlank(message = "State is required")
private String deliveryState;
@NotBlank(message = "Zip code is required")
private String deliveryZip;
@CreditCardNumber(message = "Not a valid credit card number")
private String ccNumber;
@Pattern(regexp = "^(0\[1-9\]|1\[0-2\])(\[\\/\])(\[2-9\]\[0-9\])$", message = "Must be formatted MM/YY")
private String ccExpiration;
@Digits(integer = 3, fraction = 0, message = "Invalid CVV")
private String ccCVV;
private List\<Taco\> tacos = new ArrayList\<\>();
public void addTaco(Taco taco) {
tacos.add(taco);
}
}
And here are the specific methods from the controller classes:
@PostMapping
public String processTaco(@Valid Taco taco, Errors errors, @ModelAttribute TacoOrder tacoOrder){
if (errors.hasErrors()) {
return "design"; //@Valid - perform validation on submitted Taco Object
}
tacoOrder.addTaco(taco);
log.info("Processing taco: {}", taco);
return "redirect:/orders/current";
@PostMapping
public String processOrder(@Valid TacoOrder order, Errors errors, SessionStatus sessionStatus) {
if (errors.hasErrors()) {
return "orderForm";
}
log.info("Order submitted: {}", order);
sessionStatus.setComplete();
//setComplete - ensure session cleaned up and ready for new order
return "redirect:/";
}
yet when i test it out on my localhost, it still allows me to enter in what i shouldn't be able to and doesn't return me back to the same page.
here it the orderForm.hmtl also:
<!DOCTYPE html>
Taco Cloud
<h1>Order your taco creations!</h1>
<img src="@{/TacoCloud.png}" />
<h3>Your tacos in this order:</h3>
<a href="@{/design}">Design another taco</a><br />
<ul>
<li>
taco name
</li>
</ul>
<h3>Deliver my taco masterpieces to...</h3>
Name:
<br />
Street address:
<br />
City:
<br />
State:
<br />
Zip code:
<br />
<h3>Here's how I'll pay...</h3>
Credit Card #:
CC Num Error
<br />
<label for="ccExpiration">Expiration: </label>
<input type="text" th:field="*{ccExpiration}" />
<br />
<label for="ccCVV">CVV: </label>
<input type="text" th:field="*{ccCVV}" />
<br />
<input type="submit" value="Submit Order" />
I've crossed checked each of my files to see if there was anything missing and it continues to tell me that they're fine and to double check things that seem to be right so im not sure where to go from here.
Upvotes: 0
Views: 59
Reputation: 36
I assume you are using the spring framework and want the validation for the controller to be handled automatically, please have a look here:
Spring boot validation annotations @Valid and @NotBlank not working
I would suggest you use the following dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
And as mentioned in one of the linked answers remove javax.validation.validation-api
to make sure the correct annotations are imported.
Upvotes: 0