I try to do a jsp form which would appeal to the the servlet after the Angular.js validation .
I have 3 main files -
default.jsp
<!DOCTYPE html>
<html>
<head>
<!-- CSS -->
<link rel="stylesheet" href="css/bootstrap.min.css" />
<!-- JS -->
<script src="js/angular.js"></script>
<script src="js/app.js"></script>
</head>
<body ng-app="validationApp" ng-controller="mainController">
<div class="container">
<!-- =================================================================== -->
<!-- FORM ============================================================== -->
<!-- =================================================================== -->
<!-- pass in the variable if our form is valid or invalid -->
<form action="AfterFormServlet" method="POST" name="userForm"
ng-submit="submitForm(userForm.$valid)" novalidate>
<!-- FIRST NAME -->
<div class="form-group"
ng-class="{ 'has-error' : userForm.name.$invalid && !userForm.name.$pristine }">
<label>First name</label> <input type="text" name="name"
class="form-control" ng-model="user.name"
ng-pattern="/^[a-z ,.'-]+$/i" required>
<p ng-show="userForm.name.$invalid && !userForm.name.$pristine"
class="help-block">Enter a valid last first name.</p>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</body>
</html>
AfterFormServlet.java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class AfterFormServlet
*/
@WebServlet("/AfterFormServlet")
public class AfterFormServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AfterFormServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Hi");
}
}
app.js
// create angular app
var validationApp = angular.module('validationApp', []);
// create angular controller
validationApp.controller('mainController', function($scope) {
// function to submit the form after all validation has occurred
$scope.submitForm = function(isValid) {
// check to make sure the form is completely valid
if (isValid) {
alert('Thanks, your order was Sent');
}
else {
alert('Invalid form');
}
};
});
After I run it I get into the form , fill the required fields , click on the submit and it get into the servlet even though the validation failed .
How to make it work ?
Update:
following @ug_ suggestion -
<form action="" active="AfterFormServlet" method="POST" name="userForm"
ng-submit="submitForm(userForm.$valid);" novalidate>
make the validation but doesn't get into the servlet .
And -
<form action="AfterFormServlet" method="POST" name="userForm"
ng-submit="submitForm(userForm.$valid);" novalidate>
cause the validation take affect but it get into the servlet even though the validation failed .
action=""and return true withng-submit. Also quick question, does the alert show?alertis shown .