SlideShare a Scribd company logo
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
<form name="myform" class="css-form" novalidate>
disable browser's native
form validation
Name: <input type="text" ng-model="user.name"/>
E-mail:<input type="email" ng-model="user.email" required/>
Gender:
<input type="radio" ng-model="user.gender" value="male"/> male
<input type="radio" ng-model="user.gender" value="female"/> female
</form>
<button ng-click="reset()"
ng-disabled="isUnchanged(user)">
RESET
</button>
<button ng-click="update(user)"
ng-disabled=“myform.$invalid || isUnchanged(user)">
SAVE
</button>
validation
<style type="text/css">
.css-form input.ng-invalid.ng-dirty {background-color: #FA787E;}
.css-form input.ng-valid.ng-dirty {background-color: #78FA89;}
</style>
Binding the input
element to scope
property
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
<!-- Expressions -->
Please type your name : {{name}}
<!-- Directives & Data Binding -->
Name: <input ng-model="name" value="..." />
Template
name :
Scope
value
elm.bind('keydown', … )
$scope.$watch('name', … )
Directive
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
Form
 Name
 Email
 Age
 Submit function
 Reset function
Scope
Binding
Proxies
Server
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
Data Binding
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
 ng-model-options="{
updateOn: 'default blur',
debounce: {'default': 500, 'blur': 0}
}"
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
Types
 Text
 Checkbox
 File
 Password
 Email
 URL
 Number
 Range
 Date
Validations
 novalidate
 Required
 Pattern
 Minlength
 Maxlength
 Min
 Max
Status
 $error
 $pristine
 $dirty
 $valid
 $invalid
 $touched
 $untouched
 $pending
CSS
 ng-valid
 ng-invalid
 ng-pristine
 ng-dirty
 ng-touched
 ng-untouched
 ng-pending
Events
 ng-click
 ng-change
 ng-submit
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
<input type="checkbox"
ng-model="{string}"
[name="{string}"]
[ng-true-value="{string}"]
[ng-false-value="{string}"]
[ng-change="{string}"]>
<input type="email"
ng-model="{string}"
[name="{string}"]
[required]
[ng-required="{string}"]
[ng-minlength="{number}"]
[ng-maxlength="{number}"]
[ng-pattern="{string}"]>
<input type="number"
ng-model="{string}"
[name="{string}"]
[min="{string}"]
[max="{string}"]
[required]
[ng-required="{string}"]
[ng-minlength="{number}"]
[ng-maxlength="{number}"]
[ng-pattern="{string}"]
[ng-change="{string}"]> <input type="radio"
ng-model="{string}"
value="{string}"
[name="{string}"]
[ng-change="{string}"]>
<input type="text" | type="URL"
ng-model="{string}"
[name="{string}"]
[required]
[ng-required="{string}"]
[ng-minlength="{number}"]
[ng-maxlength="{number}"]
[ng-pattern="{string}"]
[ng-change="{string}"]>
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
Form
 $error
 $pristine
 $dirty
 $pending
NgModelController
<button
ng-click="update(user)"
ng-disabled="form.$invalid || isUnchanged(user)">
SAVE
</button>
<span
ng-show="f.uEmail.$dirty && f.uEmail.$invalid">
Invalid:
<span ng-show="form.uEmail.$error.email">
This is not a valid email.</span>
</span>
NgModelController
ng-model
 $valid
 $invalid
 $submitted
 $error
 $pristine
 $dirty
 $pending
 $valid
 $invalid
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
Form Validations
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
$async
Validators
$parsers
$modelValue
ngModelController
$validators
$async
Validators
$validators
$format
ters
$view
Change
Listeners$render
Status
 $error
 $pristine
 $dirty
 $valid
 $invalid
 $touched
 $untouched
 $pending
$viewValue
$$lastCommittedViewValue
$commitViewValue
$rollbackViewValue
$$debounce
ViewValue
Commit
$setView
Value
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
ngModelController
($pristine, $dirty, $error, $valid, $invalid )
$setValidity()$setPristine()
<style type="text/css">
input.ng-invalid.ng-dirty {background-color: #FA787E;}
input.ng-valid.ng-dirty {background-color: #78FA89;}
</style>
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
app.directive('contenteditable', function() {
return {
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
// view -> model
elm.bind('blur', function() {
scope.$apply(function() {
ctrl.$setViewValue(elm.html());
});
});
// model -> view
ctrl.$render = function() {
elm.html(ctrl.$modelValue);
};
// load init value from DOM
ctrl.$setViewValue(elm.html());
}
};
});
<div ng-model="content" title="Click to edit" contentEditable="true" >Some</div>
<pre>model = {{content}}</pre>
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
Custom Binding
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
app.directive('smartFloat', function () {
return {
require: 'ngModel',
link: function (scope, elm, attrs, ctrl) {
ctrl.$parsers.unshift( function (viewValue) {
if ( FLOAT_REGEXP.test(viewValue) ) {
// it is valid
ctrl.$setValidity('float', true);
return parseFloat(viewValue.replace(',', '.'));
} else {
// it is invalid, return undefined (no model update)
ctrl.$setValidity('float', false);
return undefined;
}
});
}
};
});
<div>
Length (float):
<input type="text" ng-model="length" name="length" smart-float />{{length}}<br />
<span ng-show="form.length.$error.float">This is not a valid float number!</span>
</div>
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
mi.directive('validatePasswordCharacters', function () {
var REQUIRED_PATTERNS = [/d+/,/[a-z]+/,/[A-Z]+/,/W+/,/^S+$/];
return {
require: 'ngModel',
link: function ($scope, element, attrs, ngModel) {
ngModel.$validators.passwordCharacters = function (value) {
var status = true;
angular.forEach(REQUIRED_PATTERNS, function (pattern) {
status = status && pattern.test(value);
});
return status;
};
}
}
});
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
mi.directive('validatePasswordCharacters', function () {
var REQUIRED_PATTERNS = [/d+/,/[a-z]+/,/[A-Z]+/,/W+/,/^S+$/];
return {
require: 'ngModel',
link: function ($scope, element, attrs, ngModel) {
ngModel.$validators.passwordCharacters = function (value) {
var status = true;
angular.forEach(REQUIRED_PATTERNS, function (pattern) {
status = status && pattern.test(value);
});
return status;
};
}
}
});
<form name="myForm">
<div class="label">
<input name="myPassword" type="password" ng-model="data.password"
validate-password-characters required />
<div ng-if="myForm.myPassword.$error.required">
You did not enter a password
</div>
<div ng-if="myForm.myPassword.$error.passwordCharacters">
Your password must contain a numeric, uppercase and lowercase as
well as special characters
</div>
</div>
</form>
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
ngModule.directive('usernameAvailableValidator', function($http) {
return {
require: 'ngModel',
link: function($scope, element, attrs, ngModel) {
ngModel.$asyncValidators.usernameAvailable = function(username) {
return $http.get('/api/username-exists?u=' + username);
};
}
}
});
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
ngModule.directive('usernameAvailableValidator', function($http) {
return {
require: 'ngModel',
link: function($scope, element, attrs, ngModel) {
ngModel.$asyncValidators.usernameAvailable = function(username) {
return $http.get('/api/username-exists?u=' + username);
};
}
}
});
<form name="myForm">
<!--
first the required, pattern and minlength validators are executed
and then the asynchronous username validator is triggered...
-->
<input type="text"
class="input"
name="username"
minlength="4"
maxlength="15"
ng-model="form.data.username"
pattern="^[-w]+$"
username-available-validator
placeholder="Choose a username for yourself"
required />
<div ng-if="myForm.myUsername.$pending">
Checking Username...
</div>
</form>
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
Custom Form Validations
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
<script src="angular.js" />
<script src="angular-messages.js">
angular.module('app', ['ngMessages']);
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
<script type="text/javascript" src="angular-messages.js"></script>
<script type="text/javascript">
var ngModule = angular.module('myApp', ['ngMessages']);
</script>
<form name="myForm">
<input type="text" name="colorCode" ng-model="data.colorCode"
minlength="6" required />
<div ng-messages="myForm.colorCode.$error"
ng-if="myForm.$submitted || myForm.colorCode.$touched">
<div ng-message="required">...</div>
<div ng-message="minlength">...</div>
<div ng-message="pattern">...</div>
</div>
<nav class="actions">
<input type="submit" />
</nav>
</form>
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
<script type="text/ng-template" id="my-custom-messages">
<div ng-message="required">This field is required</div>
<div ng-message="minlength">This field is too short</div>
</script>
<form name="myForm">
<input type="email" id="email" name="myEmail" ng-model="email"
minlength="5" required />
<div ng-messages="myForm.myEmail.$error"
ng-messages-include="my-custom-messages">
<div ng-message="required">You did not enter your email</div>
<div ng-message="email">Your email address is invalid</div>
</div>
</form>
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
eyalvardi.wordpress.com
Ad

Recommended

Collections - Maps
Collections - Maps
Hitesh-Java
 
Structure in C
Structure in C
Kamal Acharya
 
Dbms mini project
Dbms mini project
Home
 
PPT-Presentation-of-E-Commerce-website-Project.pptx
PPT-Presentation-of-E-Commerce-website-Project.pptx
Himanshu883663
 
Final Year Projects (Computer Science 2013) - Syed Ubaid Ali Jafri
Final Year Projects (Computer Science 2013) - Syed Ubaid Ali Jafri
Syed Ubaid Ali Jafri
 
Inheritance in java
Inheritance in java
Lovely Professional University
 
Java arrays
Java arrays
BHUVIJAYAVELU
 
Employee Management System
Employee Management System
Monotheist Sakib
 
Java Arrays
Java Arrays
Jussi Pohjolainen
 
Python libraries
Python libraries
Prof. Dr. K. Adisesha
 
Online movie ticket booking
Online movie ticket booking
mrinnovater007
 
Data Structure Question Bank(2 marks)
Data Structure Question Bank(2 marks)
pushpalathakrishnan
 
Serialization in java
Serialization in java
Janu Jahnavi
 
Abstract method
Abstract method
Yaswanth Babu Gummadivelli
 
Database connectivity in python
Database connectivity in python
baabtra.com - No. 1 supplier of quality freshers
 
Hotel Reservation System Project
Hotel Reservation System Project
raj_qn3
 
C# Inheritance
C# Inheritance
Prem Kumar Badri
 
Online Voting System ppt
Online Voting System ppt
OECLIB Odisha Electronics Control Library
 
Book Store Management System - Database Design - 2021
Book Store Management System - Database Design - 2021
Bharat Chawda
 
L14 string handling(string buffer class)
L14 string handling(string buffer class)
teach4uin
 
Python programming : Strings
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
6. static keyword
6. static keyword
Indu Sharma Bhardwaj
 
Constructor in java
Constructor in java
Pavith Gunasekara
 
Online voting system
Online voting system
Pooja Jain
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
Hazrat Bilal
 
E learning project report (Yashraj Nigam)
E learning project report (Yashraj Nigam)
Yashraj Nigam
 
Java: Introduction to Arrays
Java: Introduction to Arrays
Tareq Hasan
 
Online voting system project
Online voting system project
snauriyal1994
 
AngularJS Directives
AngularJS Directives
Eyal Vardi
 
AngularJS Architecture
AngularJS Architecture
Eyal Vardi
 

More Related Content

What's hot (20)

Java Arrays
Java Arrays
Jussi Pohjolainen
 
Python libraries
Python libraries
Prof. Dr. K. Adisesha
 
Online movie ticket booking
Online movie ticket booking
mrinnovater007
 
Data Structure Question Bank(2 marks)
Data Structure Question Bank(2 marks)
pushpalathakrishnan
 
Serialization in java
Serialization in java
Janu Jahnavi
 
Abstract method
Abstract method
Yaswanth Babu Gummadivelli
 
Database connectivity in python
Database connectivity in python
baabtra.com - No. 1 supplier of quality freshers
 
Hotel Reservation System Project
Hotel Reservation System Project
raj_qn3
 
C# Inheritance
C# Inheritance
Prem Kumar Badri
 
Online Voting System ppt
Online Voting System ppt
OECLIB Odisha Electronics Control Library
 
Book Store Management System - Database Design - 2021
Book Store Management System - Database Design - 2021
Bharat Chawda
 
L14 string handling(string buffer class)
L14 string handling(string buffer class)
teach4uin
 
Python programming : Strings
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
6. static keyword
6. static keyword
Indu Sharma Bhardwaj
 
Constructor in java
Constructor in java
Pavith Gunasekara
 
Online voting system
Online voting system
Pooja Jain
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
Hazrat Bilal
 
E learning project report (Yashraj Nigam)
E learning project report (Yashraj Nigam)
Yashraj Nigam
 
Java: Introduction to Arrays
Java: Introduction to Arrays
Tareq Hasan
 
Online voting system project
Online voting system project
snauriyal1994
 
Online movie ticket booking
Online movie ticket booking
mrinnovater007
 
Data Structure Question Bank(2 marks)
Data Structure Question Bank(2 marks)
pushpalathakrishnan
 
Serialization in java
Serialization in java
Janu Jahnavi
 
Hotel Reservation System Project
Hotel Reservation System Project
raj_qn3
 
Book Store Management System - Database Design - 2021
Book Store Management System - Database Design - 2021
Bharat Chawda
 
L14 string handling(string buffer class)
L14 string handling(string buffer class)
teach4uin
 
Online voting system
Online voting system
Pooja Jain
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
Hazrat Bilal
 
E learning project report (Yashraj Nigam)
E learning project report (Yashraj Nigam)
Yashraj Nigam
 
Java: Introduction to Arrays
Java: Introduction to Arrays
Tareq Hasan
 
Online voting system project
Online voting system project
snauriyal1994
 

Viewers also liked (20)

AngularJS Directives
AngularJS Directives
Eyal Vardi
 
AngularJS Architecture
AngularJS Architecture
Eyal Vardi
 
AngulrJS Overview
AngulrJS Overview
Eyal Vardi
 
AngularJS Routing
AngularJS Routing
Eyal Vardi
 
AngularJS Services
AngularJS Services
Eyal Vardi
 
AngularJS Animations
AngularJS Animations
Eyal Vardi
 
AngularJS Compile Process
AngularJS Compile Process
Eyal Vardi
 
AngularJS Filters
AngularJS Filters
Eyal Vardi
 
AngularJS - $http & $resource Services
AngularJS - $http & $resource Services
Eyal Vardi
 
AngularJS Testing
AngularJS Testing
Eyal Vardi
 
Curso AngularJS - 6. formularios
Curso AngularJS - 6. formularios
Álvaro Alonso González
 
Curso AngularJS - 7. temas avanzados
Curso AngularJS - 7. temas avanzados
Álvaro Alonso González
 
Introduction to Angularjs
Introduction to Angularjs
Manish Shekhawat
 
InterBase на разных устройствах быстрый старт. 2017-03-30
InterBase на разных устройствах быстрый старт. 2017-03-30
sandy97
 
Angular JS - Javascript framework for superheroes
Angular JS - Javascript framework for superheroes
Eugenio Minardi
 
angular-formly presentation
angular-formly presentation
Annia Martinez
 
AngularJS
AngularJS
Srivatsan Krishnamachari
 
Practical AngularJS
Practical AngularJS
Wei Ru
 
Understanding Asynchronous JavaScript
Understanding Asynchronous JavaScript
jnewmanux
 
App cache vs localStorage
App cache vs localStorage
senthil_hi
 
AngularJS Directives
AngularJS Directives
Eyal Vardi
 
AngularJS Architecture
AngularJS Architecture
Eyal Vardi
 
AngulrJS Overview
AngulrJS Overview
Eyal Vardi
 
AngularJS Routing
AngularJS Routing
Eyal Vardi
 
AngularJS Services
AngularJS Services
Eyal Vardi
 
AngularJS Animations
AngularJS Animations
Eyal Vardi
 
AngularJS Compile Process
AngularJS Compile Process
Eyal Vardi
 
AngularJS Filters
AngularJS Filters
Eyal Vardi
 
AngularJS - $http & $resource Services
AngularJS - $http & $resource Services
Eyal Vardi
 
AngularJS Testing
AngularJS Testing
Eyal Vardi
 
InterBase на разных устройствах быстрый старт. 2017-03-30
InterBase на разных устройствах быстрый старт. 2017-03-30
sandy97
 
Angular JS - Javascript framework for superheroes
Angular JS - Javascript framework for superheroes
Eugenio Minardi
 
angular-formly presentation
angular-formly presentation
Annia Martinez
 
Practical AngularJS
Practical AngularJS
Wei Ru
 
Understanding Asynchronous JavaScript
Understanding Asynchronous JavaScript
jnewmanux
 
App cache vs localStorage
App cache vs localStorage
senthil_hi
 
Ad

Similar to Forms in AngularJS (20)

Data::FormValidator Simplified
Data::FormValidator Simplified
Fred Moyer
 
Angular js form validation shashi-19-7-16
Angular js form validation shashi-19-7-16
Shashikant Bhongale
 
Node.js Event Emitter
Node.js Event Emitter
Eyal Vardi
 
FormValidator::LazyWay で検証ルールをまとめよう
FormValidator::LazyWay で検証ルールをまとめよう
Daisuke Komatsu
 
angular-np-3
angular-np-3
Nishikant Taksande
 
AngularJs - Part 3
AngularJs - Part 3
Nishikant Taksande
 
What’s new in ECMAScript 6.0
What’s new in ECMAScript 6.0
Eyal Vardi
 
Gravity Forms Hooks & Filters
Gravity Forms Hooks & Filters
iamdangavin
 
Dynamics 365 Web API - CRMUG April 2018
Dynamics 365 Web API - CRMUG April 2018
Greg McMurray
 
Async & Parallel in JavaScript
Async & Parallel in JavaScript
Eyal Vardi
 
Bag Of Tricks From Iusethis
Bag Of Tricks From Iusethis
Marcus Ramberg
 
Make Everyone a Tester: Natural Language Acceptance Testing
Make Everyone a Tester: Natural Language Acceptance Testing
Patrick Reagan
 
Silex meets SOAP & REST
Silex meets SOAP & REST
Hugo Hamon
 
Beyond the Final Frontier of jQuery Selectors
Beyond the Final Frontier of jQuery Selectors
Alexander Shopov
 
Daily notes
Daily notes
meghendra168
 
EWD 3 Training Course Part 43: Using JSON Web Tokens with QEWD REST Services
EWD 3 Training Course Part 43: Using JSON Web Tokens with QEWD REST Services
Rob Tweed
 
CakePHPをさらにDRYにする、ドライケーキレシピ akiyan.com 秋田真宏
CakePHPをさらにDRYにする、ドライケーキレシピ akiyan.com 秋田真宏
Masahiro Akita
 
Virtual Madness @ Etsy
Virtual Madness @ Etsy
Nishan Subedi
 
Outsourcing 3.0: the agile way
Outsourcing 3.0: the agile way
Alexey Krivitsky
 
Make Everyone a Tester: Natural Language Acceptance Testing
Make Everyone a Tester: Natural Language Acceptance Testing
Viget Labs
 
Data::FormValidator Simplified
Data::FormValidator Simplified
Fred Moyer
 
Angular js form validation shashi-19-7-16
Angular js form validation shashi-19-7-16
Shashikant Bhongale
 
Node.js Event Emitter
Node.js Event Emitter
Eyal Vardi
 
FormValidator::LazyWay で検証ルールをまとめよう
FormValidator::LazyWay で検証ルールをまとめよう
Daisuke Komatsu
 
What’s new in ECMAScript 6.0
What’s new in ECMAScript 6.0
Eyal Vardi
 
Gravity Forms Hooks & Filters
Gravity Forms Hooks & Filters
iamdangavin
 
Dynamics 365 Web API - CRMUG April 2018
Dynamics 365 Web API - CRMUG April 2018
Greg McMurray
 
Async & Parallel in JavaScript
Async & Parallel in JavaScript
Eyal Vardi
 
Bag Of Tricks From Iusethis
Bag Of Tricks From Iusethis
Marcus Ramberg
 
Make Everyone a Tester: Natural Language Acceptance Testing
Make Everyone a Tester: Natural Language Acceptance Testing
Patrick Reagan
 
Silex meets SOAP & REST
Silex meets SOAP & REST
Hugo Hamon
 
Beyond the Final Frontier of jQuery Selectors
Beyond the Final Frontier of jQuery Selectors
Alexander Shopov
 
EWD 3 Training Course Part 43: Using JSON Web Tokens with QEWD REST Services
EWD 3 Training Course Part 43: Using JSON Web Tokens with QEWD REST Services
Rob Tweed
 
CakePHPをさらにDRYにする、ドライケーキレシピ akiyan.com 秋田真宏
CakePHPをさらにDRYにする、ドライケーキレシピ akiyan.com 秋田真宏
Masahiro Akita
 
Virtual Madness @ Etsy
Virtual Madness @ Etsy
Nishan Subedi
 
Outsourcing 3.0: the agile way
Outsourcing 3.0: the agile way
Alexey Krivitsky
 
Make Everyone a Tester: Natural Language Acceptance Testing
Make Everyone a Tester: Natural Language Acceptance Testing
Viget Labs
 
Ad

More from Eyal Vardi (20)

Why magic
Why magic
Eyal Vardi
 
Smart Contract
Smart Contract
Eyal Vardi
 
Rachel's grandmother's recipes
Rachel's grandmother's recipes
Eyal Vardi
 
Performance Optimization In Angular 2
Performance Optimization In Angular 2
Eyal Vardi
 
Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)
Eyal Vardi
 
Angular 2 NgModule
Angular 2 NgModule
Eyal Vardi
 
Upgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.x
Eyal Vardi
 
Angular 2 - Ahead of-time Compilation
Angular 2 - Ahead of-time Compilation
Eyal Vardi
 
Routing And Navigation
Routing And Navigation
Eyal Vardi
 
Angular 2 Architecture
Angular 2 Architecture
Eyal Vardi
 
Angular 1.x vs. Angular 2.x
Angular 1.x vs. Angular 2.x
Eyal Vardi
 
Angular 2.0 Views
Angular 2.0 Views
Eyal Vardi
 
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0
Eyal Vardi
 
Template syntax in Angular 2.0
Template syntax in Angular 2.0
Eyal Vardi
 
Http Communication in Angular 2.0
Http Communication in Angular 2.0
Eyal Vardi
 
Angular 2.0 Dependency injection
Angular 2.0 Dependency injection
Eyal Vardi
 
Angular 2.0 Routing and Navigation
Angular 2.0 Routing and Navigation
Eyal Vardi
 
Angular 2.0 Pipes
Angular 2.0 Pipes
Eyal Vardi
 
Angular 2.0 forms
Angular 2.0 forms
Eyal Vardi
 
Modules and injector
Modules and injector
Eyal Vardi
 
Smart Contract
Smart Contract
Eyal Vardi
 
Rachel's grandmother's recipes
Rachel's grandmother's recipes
Eyal Vardi
 
Performance Optimization In Angular 2
Performance Optimization In Angular 2
Eyal Vardi
 
Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)
Eyal Vardi
 
Angular 2 NgModule
Angular 2 NgModule
Eyal Vardi
 
Upgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.x
Eyal Vardi
 
Angular 2 - Ahead of-time Compilation
Angular 2 - Ahead of-time Compilation
Eyal Vardi
 
Routing And Navigation
Routing And Navigation
Eyal Vardi
 
Angular 2 Architecture
Angular 2 Architecture
Eyal Vardi
 
Angular 1.x vs. Angular 2.x
Angular 1.x vs. Angular 2.x
Eyal Vardi
 
Angular 2.0 Views
Angular 2.0 Views
Eyal Vardi
 
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0
Eyal Vardi
 
Template syntax in Angular 2.0
Template syntax in Angular 2.0
Eyal Vardi
 
Http Communication in Angular 2.0
Http Communication in Angular 2.0
Eyal Vardi
 
Angular 2.0 Dependency injection
Angular 2.0 Dependency injection
Eyal Vardi
 
Angular 2.0 Routing and Navigation
Angular 2.0 Routing and Navigation
Eyal Vardi
 
Angular 2.0 Pipes
Angular 2.0 Pipes
Eyal Vardi
 
Angular 2.0 forms
Angular 2.0 forms
Eyal Vardi
 
Modules and injector
Modules and injector
Eyal Vardi
 

Recently uploaded (20)

WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
Quantum AI: Where Impossible Becomes Probable
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
Priyanka Aash
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
 
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
Mastering AI Workflows with FME by Mark Döring
Mastering AI Workflows with FME by Mark Döring
Safe Software
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
Quantum AI: Where Impossible Becomes Probable
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
Priyanka Aash
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
 
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
Mastering AI Workflows with FME by Mark Döring
Mastering AI Workflows with FME by Mark Döring
Safe Software
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 

Forms in AngularJS

  • 1. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected]
  • 2. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected]
  • 3. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] <form name="myform" class="css-form" novalidate> disable browser's native form validation Name: <input type="text" ng-model="user.name"/> E-mail:<input type="email" ng-model="user.email" required/> Gender: <input type="radio" ng-model="user.gender" value="male"/> male <input type="radio" ng-model="user.gender" value="female"/> female </form> <button ng-click="reset()" ng-disabled="isUnchanged(user)"> RESET </button> <button ng-click="update(user)" ng-disabled=“myform.$invalid || isUnchanged(user)"> SAVE </button> validation <style type="text/css"> .css-form input.ng-invalid.ng-dirty {background-color: #FA787E;} .css-form input.ng-valid.ng-dirty {background-color: #78FA89;} </style> Binding the input element to scope property
  • 4. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] <!-- Expressions --> Please type your name : {{name}} <!-- Directives & Data Binding --> Name: <input ng-model="name" value="..." /> Template name : Scope value elm.bind('keydown', … ) $scope.$watch('name', … ) Directive
  • 5. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] Form  Name  Email  Age  Submit function  Reset function Scope Binding Proxies Server
  • 6. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] Data Binding
  • 7. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected]
  • 8. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected]  ng-model-options="{ updateOn: 'default blur', debounce: {'default': 500, 'blur': 0} }"
  • 9. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected]
  • 10. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected]
  • 11. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] Types  Text  Checkbox  File  Password  Email  URL  Number  Range  Date Validations  novalidate  Required  Pattern  Minlength  Maxlength  Min  Max Status  $error  $pristine  $dirty  $valid  $invalid  $touched  $untouched  $pending CSS  ng-valid  ng-invalid  ng-pristine  ng-dirty  ng-touched  ng-untouched  ng-pending Events  ng-click  ng-change  ng-submit
  • 12. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] <input type="checkbox" ng-model="{string}" [name="{string}"] [ng-true-value="{string}"] [ng-false-value="{string}"] [ng-change="{string}"]> <input type="email" ng-model="{string}" [name="{string}"] [required] [ng-required="{string}"] [ng-minlength="{number}"] [ng-maxlength="{number}"] [ng-pattern="{string}"]> <input type="number" ng-model="{string}" [name="{string}"] [min="{string}"] [max="{string}"] [required] [ng-required="{string}"] [ng-minlength="{number}"] [ng-maxlength="{number}"] [ng-pattern="{string}"] [ng-change="{string}"]> <input type="radio" ng-model="{string}" value="{string}" [name="{string}"] [ng-change="{string}"]> <input type="text" | type="URL" ng-model="{string}" [name="{string}"] [required] [ng-required="{string}"] [ng-minlength="{number}"] [ng-maxlength="{number}"] [ng-pattern="{string}"] [ng-change="{string}"]>
  • 13. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] Form  $error  $pristine  $dirty  $pending NgModelController <button ng-click="update(user)" ng-disabled="form.$invalid || isUnchanged(user)"> SAVE </button> <span ng-show="f.uEmail.$dirty && f.uEmail.$invalid"> Invalid: <span ng-show="form.uEmail.$error.email"> This is not a valid email.</span> </span> NgModelController ng-model  $valid  $invalid  $submitted  $error  $pristine  $dirty  $pending  $valid  $invalid
  • 14. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] Form Validations
  • 15. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected]
  • 16. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] $async Validators $parsers $modelValue ngModelController $validators $async Validators $validators $format ters $view Change Listeners$render Status  $error  $pristine  $dirty  $valid  $invalid  $touched  $untouched  $pending $viewValue $$lastCommittedViewValue $commitViewValue $rollbackViewValue $$debounce ViewValue Commit $setView Value
  • 17. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected]
  • 18. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] ngModelController ($pristine, $dirty, $error, $valid, $invalid ) $setValidity()$setPristine() <style type="text/css"> input.ng-invalid.ng-dirty {background-color: #FA787E;} input.ng-valid.ng-dirty {background-color: #78FA89;} </style>
  • 19. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] app.directive('contenteditable', function() { return { require: 'ngModel', link: function(scope, elm, attrs, ctrl) { // view -> model elm.bind('blur', function() { scope.$apply(function() { ctrl.$setViewValue(elm.html()); }); }); // model -> view ctrl.$render = function() { elm.html(ctrl.$modelValue); }; // load init value from DOM ctrl.$setViewValue(elm.html()); } }; }); <div ng-model="content" title="Click to edit" contentEditable="true" >Some</div> <pre>model = {{content}}</pre>
  • 20. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] Custom Binding
  • 21. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected]
  • 22. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected]
  • 23. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] app.directive('smartFloat', function () { return { require: 'ngModel', link: function (scope, elm, attrs, ctrl) { ctrl.$parsers.unshift( function (viewValue) { if ( FLOAT_REGEXP.test(viewValue) ) { // it is valid ctrl.$setValidity('float', true); return parseFloat(viewValue.replace(',', '.')); } else { // it is invalid, return undefined (no model update) ctrl.$setValidity('float', false); return undefined; } }); } }; }); <div> Length (float): <input type="text" ng-model="length" name="length" smart-float />{{length}}<br /> <span ng-show="form.length.$error.float">This is not a valid float number!</span> </div>
  • 24. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] mi.directive('validatePasswordCharacters', function () { var REQUIRED_PATTERNS = [/d+/,/[a-z]+/,/[A-Z]+/,/W+/,/^S+$/]; return { require: 'ngModel', link: function ($scope, element, attrs, ngModel) { ngModel.$validators.passwordCharacters = function (value) { var status = true; angular.forEach(REQUIRED_PATTERNS, function (pattern) { status = status && pattern.test(value); }); return status; }; } } });
  • 25. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] mi.directive('validatePasswordCharacters', function () { var REQUIRED_PATTERNS = [/d+/,/[a-z]+/,/[A-Z]+/,/W+/,/^S+$/]; return { require: 'ngModel', link: function ($scope, element, attrs, ngModel) { ngModel.$validators.passwordCharacters = function (value) { var status = true; angular.forEach(REQUIRED_PATTERNS, function (pattern) { status = status && pattern.test(value); }); return status; }; } } }); <form name="myForm"> <div class="label"> <input name="myPassword" type="password" ng-model="data.password" validate-password-characters required /> <div ng-if="myForm.myPassword.$error.required"> You did not enter a password </div> <div ng-if="myForm.myPassword.$error.passwordCharacters"> Your password must contain a numeric, uppercase and lowercase as well as special characters </div> </div> </form>
  • 26. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] ngModule.directive('usernameAvailableValidator', function($http) { return { require: 'ngModel', link: function($scope, element, attrs, ngModel) { ngModel.$asyncValidators.usernameAvailable = function(username) { return $http.get('/api/username-exists?u=' + username); }; } } });
  • 27. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] ngModule.directive('usernameAvailableValidator', function($http) { return { require: 'ngModel', link: function($scope, element, attrs, ngModel) { ngModel.$asyncValidators.usernameAvailable = function(username) { return $http.get('/api/username-exists?u=' + username); }; } } }); <form name="myForm"> <!-- first the required, pattern and minlength validators are executed and then the asynchronous username validator is triggered... --> <input type="text" class="input" name="username" minlength="4" maxlength="15" ng-model="form.data.username" pattern="^[-w]+$" username-available-validator placeholder="Choose a username for yourself" required /> <div ng-if="myForm.myUsername.$pending"> Checking Username... </div> </form>
  • 28. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] Custom Form Validations
  • 29. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected]
  • 30. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] <script src="angular.js" /> <script src="angular-messages.js"> angular.module('app', ['ngMessages']);
  • 31. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] <script type="text/javascript" src="angular-messages.js"></script> <script type="text/javascript"> var ngModule = angular.module('myApp', ['ngMessages']); </script> <form name="myForm"> <input type="text" name="colorCode" ng-model="data.colorCode" minlength="6" required /> <div ng-messages="myForm.colorCode.$error" ng-if="myForm.$submitted || myForm.colorCode.$touched"> <div ng-message="required">...</div> <div ng-message="minlength">...</div> <div ng-message="pattern">...</div> </div> <nav class="actions"> <input type="submit" /> </nav> </form>
  • 32. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] <script type="text/ng-template" id="my-custom-messages"> <div ng-message="required">This field is required</div> <div ng-message="minlength">This field is too short</div> </script> <form name="myForm"> <input type="email" id="email" name="myEmail" ng-model="email" minlength="5" required /> <div ng-messages="myForm.myEmail.$error" ng-messages-include="my-custom-messages"> <div ng-message="required">You did not enter your email</div> <div ng-message="email">Your email address is invalid</div> </div> </form>
  • 33. © 2014 All rights reserved. Tel: 054-5-767-300, Email: [email protected] eyalvardi.wordpress.com

Editor's Notes

  • #24: $setValidity(validationErrorKey, isValid) Change the validity state, and notifies the form when the control changes validity. (i.e. it does not notify form if given validator is already marked as invalid). This method should be called by validators - i.e. the parser or formatter functions. Parameters validationErrorKey – {string} – Name of the validator. the validationErrorKey will assign to $error[validationErrorKey]=isValid so that it is available for data-binding. ThevalidationErrorKey should be in camelCase and will get converted into dash-case for class name. Example: myError will result in ng-valid-my-error and ng-invalid-my-error class and can be bound to as {{someForm.someControl.$error.myError}} . isValid – {boolean} – Whether the current state is valid (true) or invalid (false).