Requirements
- Text field is required, minimum length 3, maximum length 200
- DueDate is required, has to be a valid
Property | Class | Description |
---|---|---|
$valid | ng-valid | Boolean Tells whether an item is currently valid based on the rules you placed. |
$invalid | ng-invalid | Boolean Tells whether an item is currently invalid based on the rules you placed. |
$pristine | ng-pristine | Boolean True if the form/input has not been used yet. |
$dirty | ng-dirty | Boolean True if the form/input has been used. |
Accessing Angular Form Properties
- To access the form:
<form name>.<angular property>
- To access an input:
<form name>.<input name>.<angular property>
<form name=”form” class=”form-horizontal” ng-submit=”submitForm(form.$valid)” novalidate><!– novalidate prevents HTML5 validation since we will be validating ourselves –>
<div class=”form-group” ng-class=”{ ‘has-error’ : form.Text.$invalid && !form.Text.$pristine }”>
<label>Todo</label>
<input type=”text” ng-model=”item.Text” id=”Text” name=”Text” class=”form-control” placeholder=”” ng-minlength=”3″ ng-maxlength=”200″ required>
<p ng-show=”form.Text.$invalid && !form.Text.$pristine” class=”help-block”>Todo is required.</p>
<p ng-show=”form.Text.$error.minlength” class=”help-block”>Must great then list 3 characters.</p>
<p ng-show=”form.Text.$error.maxlength” class=”help-block”>Must less then 200 characters.</p>
</div>
<div class=”form-group” >
<label>Priority</label>
<input type=”number” ng-model=”item.Priority” id=”Priority” class=”form-control” name=”Priority” placeholder=””>
</div>
<div class=”form-group” ng-class=”{ ‘has-error’ : form.DueDate.$invalid && !form.DueDate.$pristine }”>
<label>DueDate</label>
<input type=”text” ng-model=”item.DueDate” id=”DueDate” name=”DueDate” class=”form-control” placeholder=”” required>
<p ng-show=”form.DueDate.$invalid && !form.DueDate.$pristine” class=”help-block”>DueDate is required.</p>
</div>
<div class=”form-group”>
<button type=”submit” class=”btn btn-primary” ng-disabled=”userForm.$invalid”>{{action}}</button>
<a href=”#/” class=”btn”>Cancel</a>
</div>
</form>
I have CreateCtrl in my app.js then
var CreateCtrl = function ($scope, $location, Todo) {
$scope.action = “Add”;
//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) {
//then could create a new Todo
Todo.save($scope.item, function () {
$location.path(‘/’);
});
}
};
};
For the whole app source code go to here:
https://github.com/sandywebdesigner/AngularJsApp
here is what will look like: