mercredi 31 décembre 2014

Dynamic form validation in angularJS not working


I have created dynamic form, And adding the ng-required attributes dynamically to my controls. but now it's not working .


This is my JS :



var app = angular.module('birthdayToDo', []);

app.controller('main', function($scope){
$scope.Control={};
$scope.Attributes =[
{
"Name":"FirstName",
"Required":"true"
},
{
"Name":"LastName",
"Required":"false"
},
{
"Name":"Email",
"Required":"true"
},
{
"Name":"Age",
"Required":"false"
}];

$scope.submitForm = function(isValid) {

// check to make sure the form is completely valid
if (isValid) {
alert('our form is amazing');
alert($scope.Control[1]); // to check correct index
}else{
return;
}

};
});


And My HTML :



<html>

<head lang="en">

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" />
<script src="app.js"></script>

</head>

<body ng-app="birthdayToDo" ng-controller="main">
<form name="userForm" ng-submit="submitForm(userForm.$valid)" novalidate>
<table>
<tr ng-repeat="attribute in Attributes">
<td>{{attribute.Name}}</td>
<td>
<input type="text" name="Control[$index]" ng-model="Control[$index]" ng-required="{{attribute.Required}}" />
</td>
<td>
<p ng-show="userForm.Control[$index].$error.required">{{attribute.Required == "true" && attribute.Name+' Required' || ''}}</p>
</td>
</tr>
</table>
<input type="submit" value="Save" />
</form>
</body>


Plunker Link


I want to show error message when either the textbox value change or form is submitted .


But when I'm doing same thing with static form it's working. working plunker





Aucun commentaire:

Enregistrer un commentaire