JSFiddle - React, Tailwind, and code Playground
HTML
<div ng-app="myApp">
<div ng-form="theForm" my-form>
<my-input></my-input>
</div>
</div>
JavaScript
var myApp = angular.module('myApp',[]);
myApp.directive('myForm', function() {
return {
restrict : 'A',
controller : [ '$scope', '$element', '$attrs', function($scope, $element, $attrs) {
this.getForm = function() {
return $scope[$attrs['ngForm']];
}
} ]
}
});
myApp.directive('myInput', function() {
return {
restrict : 'E',
require : '^myForm',
template : 'valid: {{theForm.$valid}}'
+ '<input type="text" name="day" ng-model="person.day" ng-change="validateDate(person)"/>'
+ '<input type="text" name="month" ng-model="person.month" ng-change="validateDate(person)" />'
+ '<input type="text" name="year" ng-model="person.year" ng-change="validateDate(person)" />',
link : function(scope, element, attrs, myForm) {
console.log(scope, element, attrs, myForm.getForm());
var dateForm = myForm.getForm();
scope.person = {
day : "11",
month : "1",
year : "2014"
};
scope.validateDate = function performValidation(person) {
if (person && person.day && person.month && person.year) {
var validity = validateDay(person.day) && validateMonth(person.month) && validateYear(person.year);
dateForm.$setValidity("birthdate", validity);
}
};
var validateDay = function validateDay(day) {
if (day < 0 || day > 31) {
return false;
} else {
return true;
}
};
var validateMonth = function validateMonth(month) {
if (month < 0 || month > 12) {
return false;
} else {
return true;
}
};
var validateYear = function validateYear(year) {
if (year < 0 || year > 2200) {
return false;
} else {
return true;
}
};
}
};
});