从JQuery调用服务器端表单验证

时间:2016-12-27 11:01:08

标签: java jquery spring

我有一个Thymeleaf / Spring表单,使用@NotNull,@ Size,@ NotEmpty注释映射到DTO对象。提交后我检查是否有任何错误,并返回到表单页面或保存表单 - 非常标准的方法。有没有办法只从JQuery调用验证?我的意思是:如果from无效 - 返回false或重定向到表单页面,但是如果确定,则返回true并在那里停止,不要提交。

<form action="#" th:action="@{/form}" th:object="${businessTripDTO}" th:method="post" id="myForm">
    <fieldset class="form-group">
        <label for="idNum">Destination: </label>
        <input id="idNum" type="text" class="form-control" th:field="*{destination}" />
        <label th:if="${#fields.hasErrors('destination')}" th:errors="*{destination}" class="has-error"></label>
    </fieldset>
    <button type="submit" class="btn btn-primary">Save</button>

控制器:

@RequestMapping(value = "/form", method = RequestMethod.POST)
public String sendTripForm(@Valid BusinessTripDTO businessTripDTO, BindingResult bindingResult){

if (bindingResult.hasErrors()) {
        return "form";
}
...

来自form.html的JQuery调用submit,但应该只调用服务器端验证。

$(function () {
    $('body').on('click', '.odom-submit', function (e) {
        $('#myForm').submit();
    });
}); 

0 个答案:

没有答案
相关问题