Validation with Spring MVC

Asked

Viewed 416 times

2

You guys talk beauty? Well, I started studying Spring by Algaworks courses recently and today I came across a problem, is the following:

I have a form that shows me a list of employees, and as it was passed in the course, I also have a form for registration and editing that are the same. Dai I thought of making a modal that would serve as registration and editing form, so I searched how to do this and I ended up finding, the modal is working 90%, the validations are being made in JQuery, ta registering, showing success message and all, but, here comes the problem.

I don’t know if you know this way of validating the server with Spring, the course taught us to do so:

@RequestMapping(value="/novo", method = RequestMethod.POST)
public String salva(@Validated Funcionario funcionario, Errors errors, RedirectAttributes attributes){      
    if (errors.hasErrors()) {
        return "redirect:/saboia/funcionarios";
    }else{          
        funcionarios.save(funcionario);         
        attributes.addFlashAttribute("mensagem","Funcionario Salvo com Sucesso!");          
        return "redirect:/saboia/funcionarios";
    }
}

That is, if the form has errors as shown there in the first if, then I would redirect to the specified controller, however, the redirect would end the registration request, so that all data would be lost.

As the registration form is no longer a specific page as it was before so I could give a redirect so I was a little lost, How to make the validations performed on the server reach my modal?

Imagem do modal

  • I suggest asking in the forum itself Algaworks. If you get the answer, you can put here yourself.

  • in this case you would need to return the reply in JSON format, and make an AJAX call with jQuery, so you could receive the error messages if it occurs and display on the screen for the user.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.