I'm trying to validate one of the parameters on a get request and would like to add an error message to the view and then return the same view with the added error message when validation fails. I've spent a lot of time reading documentation and looking at examples but haven't been able to figure out how it works with Spring MVC and Thymeleaf in their current versions.
The method in question:
@GetMapping(value = "/converter", params = {"from", "to", "amount"})
public String convert(@RequestParam("from") String from,
@RequestParam("to") String to,
@RequestParam("amount") @Valid @NotBlank @DecimalMin("0.0") double amount,
Model model) {
String conversion = converterService.convert(new ConversionDTO(from, to, amount));
//model.addValidationErrorsToModelSomehow(errors);
model.addAttribute("conversion", conversion);
return "main";
}
The view:
<div>
<!-- display error here -->
<p th:text="${'Has error: ' + error}"></p>
<form method="get">
Amount
<input type="text" name="amount">
From
<select name="from">
<option value="SEK">SEK</option>
<option value="EUR">EUR</option>
<option value="CHF">CHF</option>
<option value="USD">USD</option>
</select>
To
<select name="to">
<option value="SEK">SEK</option>
<option value="EUR">EUR</option>
<option value="CHF">CHF</option>
<option value="USD">USD</option>
</select>
<input type="submit">
<input type="text" th:value="${conversion}">
</form>
</div>
What do I need to add to get it working?