Validation using Spring and Thymleaf

0

I have a page with two forms. One to make the update only the password field, and another to update two name and email fields. When this update page opens, it has the fields filled in with their proper name and email values. The password field appears empty, of course.

I'm using spring to do the validation, the problem is when I'm updating only the password field.

After validation, if there is something not valid, a message is displayed to the user on the same page, but when this message is displayed, in relation to the password field (invalid field) the name and email fields of the other form are empty. I do not want this to happen, I want to show the error message, but the name and email fields should be filled up like when the page opened.

Here's what I tried to do:

  <form action="#" method="post"  th:action="@{/usuario/update/senha}" th:object="${usuario}">
         ....

                <input type="hidden" th:field="*{id}" />
       ....
                    <div class="form-group">
                        <label class="control-label" for="senha"
                               th:text="#{usuario.senha}">senha: </label>
                        <input class="form-control" id="senha" th:field="*{senha}"  type="password"/>
                        <p class="alert alert-danger" th:if="${#fields.hasErrors('senha')}" th:errors="*{senha}">Dados incorretos</p>

             ....
             ...

            <div class="form-group">
                <button class="btn btn-default" type="submit">
                    <span class="fa fa-search"></span>
                    Salvar</button>
                <button class="btn btn-default" type="reset">
                    <span class="fa fa-search"></span>
                    Limpar </button>
            </div>
        </form> 


   <form action="#" method="post"  th:action="@{/usuario/update}" th:object="${usuario}">

              ...

                    <input type="hidden" th:field="*{id}" />

                   ...
                            <label class="control-label" for="nome"
                                   th:text="#{usuario.nome}">Nome: </label>           
                            <input class="form-control" id="nome" th:field="*{nome}"  type="text" th:value="${nome}"  name="nome"/>
                            <p class="alert alert-danger" th:if="${#fields.hasErrors('nome')}" th:errors="*{nome}">Dados incorretos</p>

                  ...

                            <label class="control-label" for="email"
                                   th:text="#{usuario.email}">Email: </label>
                            <input class="form-control" id="email" th:field="*{email}"  type="email" th:value="${email}"  name="email"/>
                            <p class="alert alert-danger" th:if="${#fields.hasErrors('email')}" th:errors="*{email}">Dados incorretos</p>

             ...

                    <button class="btn btn-default" type="submit">
                        <span class="fa fa-search"></span>
                        Salvar</button>
                    <button class="btn btn-default" type="reset">
                        <span class="fa fa-search"></span>
                        Limpar</button>
              ....
            </form> 

Here is the content of the coder method: Where I inserted two name and email objects to be sent to the page:

 @RequestMapping(value = {"/update/senha/{id}", "/update/senha"},
            method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView updateSenha(@PathVariable("id") Optional<Long> id,
            @ModelAttribute("usuario") @Validated Usuario usuario, BindingResult result) {

        ModelAndView view = new ModelAndView();

        if (id.isPresent()) {
            usuario = usuarioService.findById(id.get());
            view.addObject("usuario", usuario);
            view.setViewName("usuario/atualizar.html");
            return view;
        }
        if (result.hasFieldErrors("senha")) {
            usuario = usuarioService.findById(usuario.getId());

            view.addObject("nome", usuario.getNome());
            view.addObject("email", usuario.getEmail());
            view.setViewName("usuario/atualizar.html");
            return view;
        }
        usuarioService.updateSenha(usuario);

        view.setViewName("redirect:/usuario/perfil/" + usuario.getId());

        return view;
    }

What is the problem with the above code? How do you make this name and email objects appear as value on the page?

    
asked by anonymous 25.04.2017 / 20:30

0 answers