I am a newbie in Spring MVC/Jquery and Bootstrap. Basically, I want to submit a form through a Bootstrap modal dialog -
<div class="modal-body">
<sf:form class="form-horizontal" name="fpForm" id="fpForm" modelAttribute="fpDetails" action=“/forgotPassword.html“ htmlEscape="true" >
<div class="row">
<div class="col-md-3">
<label>Email Address : <span class="redStar">*</span></label>
</div>
<div class="col-md-9">
<sf:input type="email" class="form-control user inputIconPos" id="userName" placeholder="Email Address" path="userName"/>
<sf:errors path="userName" cssStyle="color: #ff0000;"/>
</div>
</div>
<div class="row">
<div class="col-sm-12 text-center">
<sf:button class="btn btn-primary" onclick="javascript:forgotPassword();">Send Email</sf:button>
</div>
</div>
</sf:form>
</div>
The client-side validation is taken care by Jquery validation which does an ajax post if there are no validation issues. :-
function forgotPassword() {
$("#fpForm").validate({
errorClass: "my-error-class",
rules: {
userName: {
required: true,
email: true
}
},
submitHandler: function (form) {
$(form).ajax({
type: "POST",
url: tgtUrl,
dataType: "json",
data: formdata,
success: function() {
**//NOT INVOKED**
alert(“success”);
},
error: function(e) {
**//NOT INVOKED**
alert("Error"+e);
}
});
}
});
}
The form is then handled by a Spring MVC controller which runs some server side validation checks. The validation error/success message is added to the model. It is returning a jsonView (org.springframework.web.servlet.view.json.JsonView)
@RequestMapping(value = "forgotpassword.html", method = RequestMethod.POST)
public String checkEmail(HttpServletRequest request,@ModelAttribute("fpDetails") @Valid LoginDetails fpDetails,
HttpServletResponse response,RedirectAttributes model )
{
boolean status = isValidUser(fpDetails.getUser());
if(status){
model.addFlashAttribute("status”,”Please verify through your email ”);
}else{
model.addFlashAttribute("status”,”Not a registered email”);
}
return "jsonView";
}
ISSUE: I would like to stay on the modal itself to show the validation results.
What happens right now is the page gets redirected to “GET” version of the same URL(/forgotPassword.html) and shows the JSON object? Also, the Jquery .ajax methods success or error callback does not get invoked(probably because it is redirected to ).
Hope I could convey the issue/requirement properly!