angularjs form data is not post properly.? - angularjs

My HTML form data is not POST
HTML View Page:-
<div class="col-sm-12">
<div class="card-box">
<form class="form-inline" name="addstock"
ng-submit="saveStockPurchaseInvoice()">
<h3 class="m-t-0 m-b-30 header-title text-center">Invoice
Details</h3>
<div class="row" style="margin-bottom: 20px;">
<div class="form-group col-md-3" style="">
<select name="supplier" ng-model="productStock.suppliername"
class="form-control" required="">
<option value="">Select a Supplier</option>
<option ng-repeat="allsupplier in supplier"
value="{{allsupplier.supplier_id}}">
{{allsupplier.supplier_name}}</option>
</select> <br> <span style="color: red"
ng-show="addstock.supplier.$touched && addstock.supplier.$invalid">Supplier
is Required.</span>
</div>
<div class="form-group col-md-3">
<input type="text" class="form-control"
ng-model="productStock.invoice" id="invoice" name="invoice"
placeholder="Enter Invoice Number" required> <br> <span
style="color: red"
ng-show="addstock.invoice.$touched && addstock.invoice.$invalid">Enter
Invoice Number.</span>
</div>
<div class="form-group col-md-3">
<input type="date" name="productdate" id="productdate"
ng-model="productStock.productdate" class="form-control"
required=""> <br> <span style="color: red"
ng-show="addstock.productdate.$touched && addstock.productdate.$invalid">Date
is Required.</span>
</div>
</div>
<h3 class="m-t-0 m-b-30 header-title text-center">ITEMS</h3>
<div ng-repeat="productStock in productStocks"
style="margin-bottom: 10px;">
<div class="form-group">
<!--<input type="text" name="product_name" ng-model="productsStock.product_name" required="" placeholder="Product Name" class="form-control" id="product_name">-->
<select name="productStock_name"
ng-model="productStock.productStock" class="form-control"
required="">
<option value="">Select a Product</option>
<option ng-repeat="products in allproducts"
value="{{products.product_id}}">
{{products.product_name}}</option>
</select> <br> <span style="color: red"
ng-show="addstock.productStock_name.$touched && addstock.productStock_name.$invalid">Product
Name Required.</span>
</div>
<div class="form-group">
<input id="productstock_qty" type="number" min="1" required=""
name="productstock_qty" ng-model="productStock.productstock_qty"
placeholder="Product Quantity" class="form-control"> <br>
<span style="color: red"
ng-show="addstock.productstock_qty.$touched && addstock.productstock_qty.$invalid">Product
Quantity required.</span>
</div>
<div class="form-group">
<input id="productstock_price" type="number" min="1"
ng-model="productStock.productstock_price"
name="productstock_price" placeholder="Product Price"
class="form-control" maxlength="15" size="10" required="">
<br> <span style="color: red"
ng-show="addstock.productstock_price.$touched && addstock.productstock_price.$invalid">Price
required.</span>
</div>
<div class="form-group">
<select name="product_units"
ng-model="productStock.productstock_units" class="form-control"
required="">
<option value="">Select Units</option>
<option ng-repeat="units in allunits" value="{{units.unit_id}}">
{{units.unit_name}}</option>
</select> <br> <span style="color: red"
ng-show="addstock.product_units.$touched && addstock.product_units.$invalid">Units
is required.</span>
</div>
<div class="form-group">
<input id="productstock_cgst" type="text"
ng-model="productStock.productstock_cgst" name="productstock_cgst"
placeholder="CGST" class="form-control" maxlength="10" size="6"
required=""> <br> <span style="color: red"
ng-show="addstock.productstock_cgst.$touched && addstock.productstock_cgst.$invalid">required.</span>
</div>
<div class="form-group">
<input id="productstock_sgst" type="text"
ng-model="productStock.productstock_sgst" name="productstock_sgst"
placeholder="SGST" class="form-control" maxlength="10" size="6"
required=""> <br> <span style="color: red"
ng-show="addstock.productstock_sgst.$touched && addstock.productstock_sgst.$invalid">required.</span>
</div>
<!--<div class="form-group">-->
<!-- <input id="productstock_gst" type="text" ng-model="productStock.productstock_gst" ng-keydown="keydownevt()" name="productstock_gst" placeholder="GST" class="form-control" maxlength="10" size="6" required="">-->
<!-- <br>-->
<!-- <span style="color:red" ng-show="addstock.productstock_gst.$touched && addstock.productstock_gst.$invalid">required.</span> -->
<!--</div>-->
<div class="form-group">
<input id="productstock_total" type="text"
ng-model="productStock.productstock_total"
name="productstock_total" placeholder="Total" class="form-control"
required=""> <br> <span style="color: red"
ng-show="addstock.productstock_total.$touched && addstock.productstock_total.$invalid">Total
is required</span>
</div>
<button class="btn btn-danger btn-sm" ng-show="$last"
ng-click="removeStock()">
<span class="glyphicon glyphicon-remove"></span>
</button>
</div>
<!-- ng-repeat close for add dynamic product field-->
<div class="form-group text-right m-b-0">
<button class="addfields btn btn-info waves-effect waves-light"
ng-click="addstockproduct()">Add Stock</button>
</div>
<div class="form-group text-right m-b-0 pull-right"
style="margin-top: 10px;">
<button type="submit" ng-click="" class="btn btn-primary">Save</button>
<button type="reset" class="btn btn-warning">Reset</button>
</div>
</form>
</div>
angularjs Controller:-
//Add Dynamic Fields.
$scope.productStocks = [{id: 'firstField2'}];
$scope.addstockproduct = function(){
var newItemNo2 = $scope.productStocks.length+1;
$scope.productStocks.push({'id':'field'+newItemNo2});
}
$scope.removeStock = function() {
var itemLast2 = $scope.productStocks.length-1;
$scope.productStocks.splice(itemLast2);
};
angularjs code for add records:-
$scope.productStock={};
$scope.suplier_succ = false;
$scope.suplier_err = false;
$scope.saveStockPurchaseInvoice=function(){
$http({
method: 'post',
url: 'stock/insert_stock',
data: $scope.productStock,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function (data)
{
if (data == 1) {
$scope.suplier_succ = $scope.suplier_succ ? false : true;
$scope.succ = "Stock added successfully";
$timeout(function () {
$(".modal").modal("hide");
}, 3000);
// $scope.formsup = {}; // clears input fields
// $scope.addsuppler.$setPristine();
// $scope.addsuppler.$setUntouched();
} else{
$scope.suplier_err = $scope.suplier_err ? false : true;
$scope.err = "Stock insertion failed! Try again.";
}
});
};
Codeigniter Controller:-
public function insert_stock(){
$request = json_decode(file_get_contents('php://input'), TRUE);
print_r($request);
}
enter image description here
My Result:-(All Records value is not POST)
enter image description here

I had the same problem with form data post.
I was able to resole it by setting headers: { 'Content-Type': undefined } and transformRequest: angular.identity in http request.
below is my sample code.
function Upload(containerName, objFile, objParams) {
var deferred = $q.defer();
var frmData = new FormData();
frmData.append('file', objFile);
var req = {
method: 'POST',
url: window.APIBaseUrl + 'containers/' + containerName + '/upload',
data: frmData,
params: objParams,
headers: { 'Content-Type': undefined },
transformRequest: angular.identity,
dataType: "json"
}
$http(req).then(function (response) {
deferred.resolve(response.data);
}, function (err) {
deferred.reject(handleHttpError(err));
$log.error("Error in upload", err);
});
return deferred.promise;
}

This May help you
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<div class="col-sm-6" style="width:50%;">
<div class="card-box">
<form class="form-inline" name="addstock"
ng-submit="saveStockPurchaseInvoice()">
<h3 class="m-t-0 m-b-30 header-title text-center">Invoice
Details</h3>
<div class="row" style="margin-bottom: 20px;">
<div class="form-group col-md-3" style="">
<select name="supplier" ng-model="productStock.suppliername"
class="form-control" required="">
<option value="">Select a Supplier</option>
<option ng-repeat="allsupplier in supplier"
value="{{allsupplier.supplier_id}}">
{{allsupplier.supplier_name}}</option>
</select> <br> <span style="color: red"
ng-show="addstock.supplier.$touched && addstock.supplier.$invalid">Supplier
is Required.</span>
</div>
<div class="form-group col-md-3">
<input type="text" class="form-control"
ng-model="productStock.invoice" id="invoice" name="invoice"
placeholder="Enter Invoice Number" required> <br> <span
style="color: red"
ng-show="addstock.invoice.$touched && addstock.invoice.$invalid">Enter
Invoice Number.</span>
</div>
<div class="form-group col-md-3">
<input type="date" name="productdate" id="productdate"
ng-model="productStock.productdate" class="form-control"
required=""> <br> <span style="color: red"
ng-show="addstock.productdate.$touched && addstock.productdate.$invalid">Date
is Required.</span>
</div>
</div>
<h3 class="m-t-0 m-b-30 header-title text-center">ITEMS</h3>
<div ng-repeat="item in productStocks track by $index"
style="margin-bottom: 10px;">
<div class="form-group">
<!--<input type="text" name="product_name" ng-model="productsStock.product_name" required="" placeholder="Product Name" class="form-control" id="product_name">-->
<select name="productStock_name"
ng-model="productStock.items[$index].productStock" class="form-control"
required="">
<option value="">Select a Product</option>
<option ng-repeat="products in allproducts"
value="{{products.product_id}}">
{{products.product_name}}</option>
</select> <br> <span style="color: red"
ng-show="addstock.productStock_name.$touched && addstock.productStock_name.$invalid">Product
Name Required.</span>
</div>
<div class="form-group">
<input id="productstock_qty" type="number" min="1" required=""
name="productstock_qty" ng-model="productStock.items[$index].productstock_qty"
placeholder="Product Quantity" class="form-control"> <br>
<span style="color: red"
ng-show="addstock.productstock_qty.$touched && addstock.productstock_qty.$invalid">Product
Quantity required.</span>
</div>
<div class="form-group">
<input id="productstock_price" type="number" min="1"
ng-model="productStock.items[$index].productstock_price"
name="productstock_price" placeholder="Product Price"
class="form-control" maxlength="15" size="10" required="">
<br> <span style="color: red"
ng-show="addstock.productstock_price.$touched && addstock.productstock_price.$invalid">Price
required.</span>
</div>
<div class="form-group">
<select name="product_units"
ng-model="productStock.items[$index].productstock_units" class="form-control"
required="">
<option value="">Select Units</option>
<option ng-repeat="units in allunits" value="{{units.unit_id}}">
{{units.unit_name}}</option>
</select> <br> <span style="color: red"
ng-show="addstock.product_units.$touched && addstock.product_units.$invalid">Units
is required.</span>
</div>
<div class="form-group">
<input id="productstock_cgst" type="text"
ng-model="productStock.items[$index].productstock_cgst" name="productstock_cgst"
placeholder="CGST" class="form-control" maxlength="10" size="6"
required=""> <br> <span style="color: red"
ng-show="addstock.productstock_cgst.$touched && addstock.productstock_cgst.$invalid">required.</span>
</div>
<div class="form-group">
<input id="productstock_sgst" type="text"
ng-model="productStock.items[$index].productstock_sgst" name="productstock_sgst"
placeholder="SGST" class="form-control" maxlength="10" size="6"
required=""> <br> <span style="color: red"
ng-show="addstock.productstock_sgst.$touched && addstock.productstock_sgst.$invalid">required.</span>
</div>
<!--<div class="form-group">-->
<!-- <input id="productstock_gst" type="text" ng-model="productStock.productstock_gst" ng-keydown="keydownevt()" name="productstock_gst" placeholder="GST" class="form-control" maxlength="10" size="6" required="">-->
<!-- <br>-->
<!-- <span style="color:red" ng-show="addstock.productstock_gst.$touched && addstock.productstock_gst.$invalid">required.</span> -->
<!--</div>-->
<div class="form-group">
<input id="productstock_total" type="text"
ng-model="productStock.items[$index].productstock_total"
name="productstock_total" placeholder="Total" class="form-control"
required=""> <br> <span style="color: red"
ng-show="addstock.productstock_total.$touched && addstock.productstock_total.$invalid">Total
is required</span>
</div>
<button class="btn btn-danger btn-sm" ng-show="$last"
ng-click="removeStock()">
<span class="glyphicon glyphicon-remove"></span>
</button>
</div>
<!-- ng-repeat close for add dynamic product field-->
<div class="form-group text-right m-b-0">
<button class="addfields btn btn-info waves-effect waves-light"
ng-click="addstockproduct()">Add Stock</button>
</div>
<div class="form-group text-right m-b-0 pull-right"
style="margin-top: 10px;">
<button type="submit" ng-click="" class="btn btn-primary">Save</button>
<button type="reset" class="btn btn-warning">Reset</button>
</div>
</form>
</div>
</div>
<div style="width:50%">
<pre>{{productStock | json}}</pre>
</div>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope,$http) {
$scope.productStocks = [{id: 'firstField2'}];
$scope.supplier = [{supplier_id:1, supplier_name:'abc'}];
$scope.allproducts = [{ product_name:'qwerty',product_id:1 }];
$scope.allunits = [{ unit_id:1,unit_name:'iop'}];
$scope.addstockproduct = function(){
var newItemNo2 = $scope.productStocks.length+1;
$scope.productStocks.push({'id':'field'+newItemNo2});
}
$scope.removeStock = function() {
var itemLast2 = $scope.productStocks.length-1;
$scope.productStocks.splice(itemLast2);
};
$scope.productStock={};
$scope.suplier_succ = false;
$scope.suplier_err = false;
$scope.saveStockPurchaseInvoice=function(){
$http({
method: 'POST',
url: 'http://abcd/insert', //change to your url
data: JSON.stringify($scope.productStock),
//headers: {'Content-Type': 'application/x-www-form-urlencoded'}
headers: { 'Content-Type': undefined },
transformRequest: angular.identity,
dataType: "json"
// 'Content-Type':'application/json'
}).then(function (response)
{
if (data == 1) {
$scope.suplier_succ = $scope.suplier_succ ? false : true;
$scope.succ = "Stock added successfully";
$timeout(function () {
$(".modal").modal("hide");
}, 3000);
// $scope.formsup = {}; // clears input fields
// $scope.addsuppler.$setPristine();
// $scope.addsuppler.$setUntouched();
} else{
$scope.suplier_err = $scope.suplier_err ? false : true;
$scope.err = "Stock insertion failed! Try again.";
}
});
};
//$request = json_decode(file_get_contents('php://input'), TRUE);
// print_r($request);
});
</script>
</body>
</html>

Related

AngularJS and ASP.NET MVC data insert issue

I am inserting Data using AngularJS and ASP.NET MVC.
But I am facing issue that the data is inserted null.
What should I do?
This is AngularJS file:
var app = angular.module("myApp", []);
app.controller("myCtrl", function ($scope, $http) {
debugger;
$scope.InsertData = function () {
var Action = document.getElementById("btnSave").getAttribute("value");
if (Action == "Submit") {
$scope.User = {};
$scope.User.Username = $scope.Username;
alert($scope.Username);
$scope.User.Password = $scope.Password;
$scope.User.Email = $scope.Email;
$scope.User.Phone = $scope.Phone;
$scope.User.DOB = $scope.DOB;
$http({
method: "post",
url: "http://localhost:2776/Account/Insert_Employee",
datatype: "json",
data: JSON.stringify($scope.User)
}).then(function (response) {
alert("inserted");
});
}
};
});
This is HTML file:
<div class="tab-content" ng-controller="myCtrl">
<div class="tab-pane active" id="register">
<h3>Register Now !!!</h3>
<p class="text-muted">Be cool and join today. Meet millions</p>
<!--Register Form-->
<form name="registration_form" id='registration_form' class="form-inline">
<div class="row">
<div class="form-group col-xs-6">
<label for="Username" class="sr-only">User Name</label>
<input id="Username" ng-model="Username" class="form-control input-group-lg" type="text" name="Username" title="Enter User name" placeholder="User name" />
</div>
</div>
<div class="row">
<div class="form-group col-xs-12">
<label for="email" class="sr-only">Email</label>
<input ng-model="Email" id="email" class="form-control input-group-lg" type="text" name="Email" title="Enter Email" placeholder="Your Email" />
</div>
</div>
<div class="row">
<div class="form-group col-xs-12">
<label for="Phone" class="sr-only">Phone</label>
<input ng-model="Phone" id="Phone" class="form-control input-group-lg" type="text" name="Phone" title="Enter Phone" placeholder="Your Phone" />
</div>
</div>
<div class="row">
<div class="form-group col-xs-12">
<label for="password" class="sr-only">Password</label>
<input ng-model="Password" id="password" class="form-control input-group-lg" type="password" name="password" title="Enter password" placeholder="Password" />
</div>
</div>
<div class="row">
<p class="birth"><strong>Date of Birth</strong></p>
<div class="form-group col-sm-3 col-xs-6">
<label for="month" class="sr-only"></label>
<select class="form-control" id="day">
<option value="Day" disabled selected>Day</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
<option>27</option>
<option>28</option>
<option>29</option>
<option>30</option>
<option>31</option>
</select>
</div>
<div class="form-group col-sm-3 col-xs-6">
<label for="month" class="sr-only"></label>
<select class="form-control" id="month">
<option value="month" ng-model="DOB" disabled selected>Month</option>
<option>Jan</option>
<option>Feb</option>
<option>Mar</option>
<option>Apr</option>
<option>May</option>
<option>Jun</option>
<option>Jul</option>
<option>Aug</option>
<option>Sep</option>
<option>Oct</option>
<option>Nov</option>
<option>Dec</option>
</select>
</div>
<div class="form-group col-sm-6 col-xs-12">
<label for="year" class="sr-only"></label>
<select class="form-control" id="year">
<option value="year" disabled selected>Year</option>
<option>2000</option>
<option>2001</option>
<option>2002</option>
<option>2004</option>
<option>2005</option>
<option>2006</option>
<option>2007</option>
<option>2008</option>
<option>2009</option>
<option>2010</option>
<option>2011</option>
<option>2012</option>
</select>
</div>
</div>
<div class="form-group gender">
<label class="radio-inline">
<input type="radio" name="optradio" checked>Male
</label>
<label class="radio-inline">
<input type="radio" name="optradio">Female
</label>
</div>
</form><!--Register Now Form Ends-->
<p>Already have an account?</p>
<button id="btnSave" value="Submit" class="btn btn-primary" ng-click="InsertData()">Register Now</button>
</div>
</div>
This is ASP.NET controller:
public string Insert_Employee(User User)
{
if (User != null)
{
using (Database1Entities Obj = new Database1Entities())
{
Obj.Users.Add(User);
Obj.SaveChanges();
return "Employee Added Successfully";
}
}
else
{
return "Employee Not Inserted! Try Again";
}
}
I am entering data, but data is inserted as null into db. What should I do no?
I have added <body ng-app="myApp">
According to me there is no error
can anyone help?
Try to wrote in this way your ajax call:
$http({
method: "post",
url: "http://localhost:2776/Account/Insert_Employee",
datatype: "json",
data: {user:$scope.User}
}).then(function (response) {
alert("inserted");
});

login validation and credentials using angularjs

I am new to angularjs. I have created simple login page. The code is working fine, but my validation and credentials have some problem. I found some clues in few sites, but those answers din't provide the proper information for my solution. The error that I am facing is in the validation. When I enter the invalid mailid and password it should pop-up error message . But I could not achieve it.
HTML :
`<form ng-submit="loginform()" class="ng-scope ng-pristine ng-valid center" name="logform"><br/><br>`
<div class="form-group col-md-4">
<label form="emailinput"><b>Email</b></label>
<input type="email" class="form-control" name="uname" id="emailinput" placeholder="you#example.com" ng-model="username" ng-pattern="emailFormat">
<span ng-show="logform.uname.$dirty.username && loginform.uname.$invalid"></span>
<span style="color: Red" ng-show="logform.uname.$valid">* Email is required</span>
<span style="color: Red" ng-show="logform.uname.$error.username">* Invalid ID </span>
</div>
<div class="form-group col-md-4">
<label form="pwdinput"><b>Password</b></label>
<input type="password" class="form-control" name="pwd" id="pwdinput" placeholder="*******" ng-model="password">
<span style="color: red" ng-show="logform.pwd.$dirty && loginform.pwd.$invalid"></span>
<span ng-show="logform.pwd.$error.$valid">* Password is required</span>
<span ng-show="logform.pwd.$error.pwd">* Invalid Password</span>
</div>
<div class="col-md-4">
<button type="cancel" class="btn" ng-click="toggle_cancel()">Cancel</button>
<button class="btn btn-primary" ng-click="submit()" ng-Disabled="logform.username.$dirty && loginform.username.$invalid || logform.pwd.$dirty && loginform.pwd.$invalid">Login</button>
</div>
AngularJs :
`var app = angular.module('logapp',['toastr']);`
app.factory('authentication', function() {
return {
isAuthenticated: false,
user: null
}
});
app.controller('credientials', function($scope,toastr,authentication) {
$scope.loginform = function (username, password) {
if ($scope.username === 'admin#evol.com' && $scope.password === '123') {
authentication.isAuthenticated = true;
$location.path("/home");
} else {
toastr.error('Invalid username and password');
}
};
});
Kindly read the snippet below and alter your code to setValidity() based on condition.
var app = angular.module('logapp', []);
app.factory('authentication', function() {
return {
isAuthenticated: false,
user: null
}
});
app.controller('credientials', function($scope, authentication) {
$scope.loginform = function(isValid) {
$scope.logform.uname.$setValidity("username", true);
$scope.logform.pwd.$setValidity("pwd", true);
if (isValid) {
if ($scope.username == 'admin#evol.com' && $scope.password == '123') {
alert("success");
authentication.isAuthenticated = true;
} else {
$scope.logform.uname.$setValidity("username", false);
$scope.logform.pwd.$setValidity("pwd", false);
}
}
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body ng-app="logapp" ng-controller="credientials">
<form ng-submit="loginform(logform.$valid)" class="ng-scope ng-pristine ng-valid center" name="logform" novalidate role="form">
<div class="form-group col-md-4">
<label form="emailinput"><b>Email</b></label>
<input type="email" class="form-control" name="uname" id="emailinput" placeholder="you#example.com" ng-model="username" ng-pattern="emailFormat" required>
<span style="color: Red" ng-show="logform.$submitted && logform.uname.$error.required">* Email is required</span>
<span style="color: Red" ng-show="logform.$submitted && logform.uname.$error.username">* Invalid ID </span>
</div>
<div class="form-group col-md-4">
<label form="pwdinput"><b>Password</b></label>
<input type="password" class="form-control" name="pwd" id="pwdinput" placeholder="*******" ng-model="password" required>
<span ng-show="logform.$submitted && logform.pwd.$error.required">* Password is required</span>
<span ng-show="logform.$submitted && logform.pwd.$error.pwd">* Invalid Password</span>
</div>
<div class="col-md-4">
<button type="cancel" class="btn" ng-click="toggle_cancel()">Cancel</button>
<button class="btn btn-primary" type="submit">Login</button>
</div>
</form>
</body>

null value passed from angularjs frontend to jersey backend

i am using angular front end and java jersey as back end .when i submit data from the form ,null values are passed instead of the actual form data. This is my form
<div id="main">
<h1>Create Leave</h1>
<form class="form-horizontal" >
<div class="form-group">
<label for="employeeName" class="col-sm-3 control-label">Employee Name</label>
<div class="col-sm-6">
<input type="text" id="num" class="form-control" ng-model="num" />
</div>
<div class="col-sm-3"></div>
</div>
<div class="form-group">
<label for="leaveType" class="col-sm-3 control-label">Leave Type</label>
<div class="col-sm-2">
<select id="leaveType" class="form-control" ng-model="leaveType">
<option value="hospital">Hospital</option>
<option value="l1">leave type 2</option>
<option value="l2">leave type 3</option>
<option value="l3">leave type 4</option>
<option value="l4">leave type 5</option>
<option value="l5">leave type 6</option>
</select>
</div>
<div class="col-sm-7"></div>
</div>
<div class="form-group">
<label for="leaveStartDate" class="col-sm-3 control-label">Leave Start Date</label>
<div class="col-sm-2">
<input type="date" id="startDate" class="form-control" ng-model="startDate" />
</div>
<div class="col-sm-7"></div>
</div>
<div class="form-group">
<label for="leaveEndDate" class="col-sm-3 control-label">Leave End Date</label>
<div class="col-sm-2">
<input type="date" id="endDate" class="form-control" ng-model="endDate" />
</div>
<div class="col-sm-7"></div>
</div>
<div class="form-group">
<div class="col-sm-3"></div>
<div class="col-sm-2">
<span><b>Is Half Day leave</b></span>
<div class="radio">
<label><input value="Yes" type="radio" name="halfDay" ng-model="isHalfDay" />Yes</label>
</div>
<div class="radio">
<label><input value="No" type="radio" name="halfDay" ng-model="isHalfDay" />No</label>
</div>
</div>
</div>
<button ng-click="add()" class="btn btn-primary">Submit</button>
<button ng-click="resetForm()" class="btn">Reset</button>
</form>
and this is the controller
app.controller("MyAddController", function($scope, $http) {
//$scope.test = {};
$scope.add = function() {
// console.log("------------>"+JSON.stringify($scope.test));
$http.post("webapi/blog/create", {
params: {
signum: $scope.num,
leaveType: $scope.leaveType,
startDate: $scope.startDate,
endDate: $scope.endDate,
isHalfDay: $scope.isHalfDay
}
}).success(function(data, status, headers, config) {
if (data) {
$scope.data = data;
alert("success");
}
}).error(function(data, status, headers, config) {
alert("error");
})
}
});
and the jersey code
#POST
#Path("create")
#Produces({ "application/json" })
public String create(#BeanParam LeaveDetails ld) {
System.out.println("Entered here");
System.out.println(ld.getIsHalfDay());
System.out.println(ld.getNum());
System.out.println(ld.getEndDate());
System.out.println(ld.getStartdate());
System.out.println(ld.getLeaveType());
new AddLeaveDao().addDetails(ld);
System.out.println("Returned here");
return "{}";
}
Please help

Angular Js Validation on Partial Views

Index Page
#model AngularMVC.Models.Employee
#{
ViewBag.Title = "Home Page";
}
<style>
input.ng-touched.ng-invalid {
border: 1px solid red;
}
input.ng-touched.ng-valid
{
border: 1px solid #48bb00;
}
li{
border:1px solid blue;
font-size:2em;
color:Green;
}
</style>
<script src="~/Scripts/angular.min.js"></script>
<script src="~/Scripts/angular-route.min.js"></script>
<script src="~/Scripts/Custom/modules.js"></script>
<div ng-app="myApp">
<form ng-controller="validateCtrl"
name="myForm" novalidate ng-submit="submit()">
<span ng-show="isViewLoading">
<img src="~/Content/images/box.gif" /> loading...
</span>
<div class="{{status}}">{{message}}</div>
<p>
<b>Name:</b> <br>
#Html.TextBoxFor(m => m.Name, new { #class = "form-control", #ng_required = "true", ng_model = "Model.name", Name = "name" })
<span style="color:red" ng-show="myForm.name.$dirty && myForm.name.$invalid">
<span ng-show="myForm.name.$error.required">Username is required.</span>
</span>
</p>
<p>
<b>Email:</b><br>
#Html.TextBoxFor(m => m.Email, new { #class = "form-control", #ng_required = "true", ng_model = "Model.email", Name = "email", type = "email" })
<span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid">
<span ng-show="myForm.email.$error.required">Email is required.</span>
<span ng-show="myForm.email.$error.email">Invalid email address.</span>
</span>
</p>
<p>
<b>Password:</b><br />
#Html.PasswordFor(m => m.Password, new { ng_model = "Model.password", #ng_required = "true", #class = "form-control", ng_minlength = "8", ng_maxlength = "16", Name = "password" })
<span style="color:red" ng-show="myForm.password.$dirty && myForm.password.$invalid">
<span ng-show="myForm.pass.$error.required">Password is required.</span>
<span ng-show="myForm.password.$touched && myForm.password.$error.minlength">Passord must be 8-16 character</span>
<span ng-show="myForm.password.$touched && myForm.password.$error.maxlength">Passord must be 8-16 character</span>
</span>
</p>
<p>
<input type="submit" ng-disabled="myForm.$invalid" value="Submit">
</p>
</form>
#*<p>Main</p>
First*#
Second
<div class="panel panel-default">
<div class="panel-heading">
{{PanelHeading}}
</div>
<div ng-view class="panel-body">
</div>
</div>
</div>
Module.js
var app = angular.module('myApp', ['ngRoute']);
app.controller('validateCtrl', function ($scope, $http) {
$scope.submit = function () {
//$scope.model = #Html.Raw(Json.Encode(Model));
$scope.isViewLoading = true;
$http({
method: "post",
url: "/Home/Create",
data: { emp: $scope.Model }
}).success(function (data, status, header, config) {
if (data = "Success") {
$scope.status = "text-success";
$scope.message = "Data successfully submitted";
}
else {
$scope.status = "text-warning";
$scope.message = "Failed to insert Data";
}
}).error(function (data, status, header, config) {
$scope.status = "text-danger";
$scope.message = 'Unexpected Error while saving data!!';
});
$scope.isViewLoading = false;
}
});
// Routes Provider for SPA
app.config(function ($routeProvider) {
$routeProvider
.when('/first', {
templateUrl: '/Home/first',
controller: 'HomeController',
})
.when('/second', {
templateUrl: '/Home/Second',
controller: 'HomeController'
})
});
Partial View Having a signup form
<div>
<form action="/Home/SignUp" name="signupForm">
<p>
<b>First Name:</b><br />
<input type="text" class="form-control" ng-required="true" ng-model="fname" name="fname" />
<span ng-show="signupForm.fname.$dirty&&signupForm.fname.$invalid" style="color:red">
<span ng-show="signupForm.fname.$error.required">First Name is required</span>
</span>
</p>
<p>
<b>Last Name:</b><br />
<input type="text" class="form-control" ng-required="true" ng-model="lname" name="lname"/>
<span ng-show="signupForm.lname.$dirty&&signupForm.lname.$invalid" style="color:red">
<span ng-show="signupForm.lname.$error.required">Last Name is required</span>
</span>
</p>
<p>
<b>Email:</b><br />
<input type="email" class="form-control" ng-required="true" name="email" ng-model="email"/>
<span ng-show="signupForm.email.$dirty&&signupForm.email.$invalid" style="color:red">
<span ng-show="signupForm.email.$error.required">Email is required</span>
<span ng-show="signupForm.email.$error.email"></span>
</span>
</p>
<p>
<b>Age:</b><br />
<input type="number" class="form-control" ng-required="true" ng-model="age"/>
<span ng-show="signupForm.age.$dirty&&signupForm.age.$invalid" style="color:red">
<span ng-show="signupForm.age.$error.required">Age is required</span>
<span ng-show="signupForm.age.$error.max ||signupForm.age.$error.min ">Age must be in between 18- 60 years</span>
</span>
</p>
<p>
<b>Password:</b><br />
<input type="password" class="form-control" ng-required="true" ng-model="password" name="password" ng-maxlength="16" ng-minlength="8"/>
<span ng-show="signupForm.password.$dirty&&signupForm.password.$invalid" style="color:red">
<span ng-show="signupForm.password.$error.required">Passowrd is required</span>
<span ng-show="signupForm.password.$error.maxlength ||signupForm.password.$error.minlength "></span>
</span>
</p>
<p>
<b>Confirm Password:</b><br />
<input type="password" class="form-control" ng-required="true" ng-model="conPassword" name="conPassword" ng-maxlength="16" ng-minlength="8" ng-equal="password"/>
<span ng-show="signupForm.conPassword.$dirty&&signupForm.conPassword.$invalid" style="color:red">
<span ng-show="signupForm.conPassword.$error.required">Passowrd is required</span>
<span ng-show="signupForm.conPassword.$error.maxlength ||signupForm.conPassword.$error.minlength "></span>
<span ng-show="signupForm.conPassword.$error.equal">Password do not match</span>
</span>
</p>
<p>
<b>Upload Profile Picture:</b><br />
<input type="file" value="Upload" /><input type="submit" value="Upload" ng-click="Upload()" ng-required="true"/>
</p>
<input type="submit" value="Submit" class="btn btn-default" ng-disabled="signupForm.$invalid"/>
</form>
I am Loading Partial View inside in ng-view in Index page but angular validation is not working inside the ng-view partial view . Please help anyone?

AngularJs text filed value undefined

I have one start date field (date picker) and a client status (drop down) fields.
If I select a date in the date picker, when passing date value to server it is showing undefined, but if I type any value with date in that field, that exact value is correctly sent to the server.
Inside the drop down list I am not getting the value it is coming undefined.
My code is:
<div class="form-group">
<label for="inputPassword3" class="col-sm-3 control-label">Client Name</label>
<div class="col-sm-9">
<div ng-controller="MyCntrl" >
<select class="form-control" name="clientname" id="clientname" class="form-control" ng-model="vm.clientname" ng-change="change(myColor)" >
<option value="">Select Client Name</option>
<option ng-repeat="color in colors"
value="{{color.value}}"
ng-selected="{{color.value == myColor}}" name="clientname" id="clientname" class="form-control" ng-model="vm.clientname">
{{color.clientname}}
</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-3 control-label">Project Startdate</label>
<div class="col-sm-9">
<!-- <input type="text" name="prjstartdate" id="prjstartdate" class="form-control" ng-model="vm.user.prjstartdate" required />-->
<div class="container" id="sandbox-container">
<div class="input-daterange input-group" id="datepicker" name="prjstartdate" id="prjstartdate" class="form-control" ng-model="vm.prjstartdate">
<input type="text" name="prjstartdate" id="prjstartdate" class="form-control" ng-model="vm.prjstartdate" required />
</div>
</div>
<div class="modal-footer" ng-controller="MyCntrlsave">
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" ng-click="savedetails()">Save changes</button>
</div>
</div>
</div>
js code:
<script type="text/javascript">
function MyCntrlsave($scope,$http) {
$scope.savedetails = function() {
var vm = this;
var projectname=$scope.vm.prjname;
var cilentname=$scope.vm.color;
var clientstatus=$scope.vm.prjsta;
var prjstartdate=$scope.vm.prjstartdate;
var prjenddate=$scope.vm.prjenddate;
var prjurl=$scope.vm.prjurl;
alert("projectname::"+projectname)
alert("prjstartdate::"+prjstartdate)
}
}

Resources