Internal error when using th: field and th: name thymeleaf and springmvc validation

0

I'm trying to validate fields to save them in the DB, I'm using @Valid and BindingResult plus Model, it already saves correctly in the DB just missing validate (which comes first),

The problem is that the BindingResult does trap the errors, but when I return to the form and tell the user that there is an error, it marks an internal error.

Entity code

@Entity
@Table
@Getter
@Setter
@ToString
public class City {
private static final long serialVersionUID = 1L;

@Column
@NotNull
@Id
private Integer id_city;

@NotNull
@Column
private String name;

@NotNull
@Column
private Boolean capital;

@NotNull
@Column
private Integer id_country;
}

Driver code

@RequestMapping(value = "/nuevoCiudad", method = RequestMethod.POST)
public String addCity(@Valid City ciudad, BindingResult bindingres, final Model model,
        @RequestParam(name = "id_city_") final int id_city, @RequestParam("name_") final String name,
        @RequestParam("capital_") final Boolean capital, @RequestParam("id_country_2set") final int id2set) {
    if (bindingres.hasErrors()) {
        System.out.println("Error de validacion");
        return "agregarCiudad";
    }
    cityService.guarda(new CityDTO(id_city, name, capital, id2set));
    model.addAttribute("countriesList", countryService.recuperaTodo());
    return "CountryHome";
}

Sight code

<html xmlns:th="http://www.thymeleaf.org">
....
<form class="w3-container w3-white" th:action="@{nuevoCiudad}"
                    th:method="post" th:object="${ciudad}">
                    <br>
                    <p>
                        <label class="w3-text-blue"><b>ID</b></label> <input
                            class="w3-input w3-border w3-sand" th:name="id_city_"
                            type="number" th:value="${idgo}" th:field="*{id_city}"> 
                            <td th:if="${#fields.hasErrors('id_city')}" th:errors="*{id_city}">Name Error</td>
                            <input
                            class="w3-input w3-border w3-sand" th:name="id_country_2set"
                            type="hidden" th:value="${id_set}">
                    <p>
                        <label class="w3-text-blue"><b>NOMBRE</b></label> <input
                            class="w3-input w3-border w3-sand" th:name="name_" type="text"
                            th:field="*{name}">
                        <td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name
                            Error</td>
                    </p>
                    <p>
                        <label class="w3-text-blue"><b>ES CAPITAL</b></label> <select
                            th:name="capital_" class="custom-select">
                            <option value="true">SI</option>
                            <option value="false">NO</option>
                        </select>
                    </p>
                    <p>
                        <br>
                        <button class="w3-btn w3-red">AGREGAR</button>
                        <button class="w3-btn w3-teal" th:formaction="@{country}">CANCELAR</button>
                    </p>
                    <br>
                </form>

Error

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this 
as a fallback.

Fri Sep 07 14:54:45 CDT 2018
There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: "class path resource 
[templates/agregarCiudad.html]")

Thanks, I hope you can help me.

    
asked by daniel hernandez 07.09.2018 в 21:56
source

0 answers