Skip to content
This repository has been archived by the owner on Jul 1, 2020. It is now read-only.

Commit

Permalink
Fix checkFormValidity formName #135 #139 #140 #141
Browse files Browse the repository at this point in the history
CheckFormValidity was not working in all cases, there was a problem
inside the function `getElementParentForm( )` which was returning null
very often. This is due to the fact that `.form` only works with `input`
element and so if user had validation on let say a `<div>` or any other
Angular element that isn't an input then the `getElementParentForm( )`
was returning null which was in turn making the
`checkFormValidity($scope.formName)` and `$validationSummary` not
working correctly.
  • Loading branch information
ghiscoding committed Dec 15, 2016
1 parent bd3dd6e commit 032c5ff
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 9 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-validation-ghiscoding",
"version": "1.5.10",
"version": "1.5.11",
"author": "Ghislain B.",
"description": "Angular-Validation Directive and Service (ghiscoding)",
"main": [
Expand Down
2 changes: 2 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Angular-Validation change logs

1.5.11 (2016-12-15) Fix checkFormValidity with formName argument, issues #135 #139 #140 #141
1.5.10 (2016-12-13) Fix UI Router issue by adding a UI Router watch on $stateChangeStart, issue #137
1.5.9 (2016-10-14) Updated main entry at package.json to work with WebPack, issue #128
1.5.7 (2016-10-03) Add Dutch and Romanian #134
1.5.6 (2016-09-24) Dates validator now checks for leap year & full date calendar, fix #130
Expand Down
6 changes: 3 additions & 3 deletions dist/angular-validation.min.js

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions more-examples/addon-3rdParty-withScope/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

var myApp = angular.module('myApp', ['ghiscoding.validation', 'pascalprecht.translate', 'ngTagsInput', 'angularjs-dropdown-multiselect',
'hljs', 'isteven-multi-select']);

myApp.config(['$compileProvider', function ($compileProvider) {
$compileProvider.debugInfoEnabled(false);
}])
.config(['$translateProvider', function ($translateProvider) {
$translateProvider.useStaticFilesLoader({
prefix: 'https://rawgit.com/ghiscoding/angular-validation/master/locales/validation/',
suffix: '.json'
});
// load English ('en') table on startup
$translateProvider.preferredLanguage('en').fallbackLanguage('en');
}]);

myApp.controller('Ctrl', ['$scope','ValidationService', function ($scope,ValidationService) {


var validationService = new ValidationService({ scope: $scope, isolatedScope: $scope });

$scope.select1model = [];
$scope.select1data = [
{id: 1, label: "Joe"},
{id: 2, label: "John"},
{id: 3, label: "Jane"}
];

$scope.submit = function () {
if (validationService.checkFormValidity($scope.test)) {
alert('valid');
}
};
}]);
73 changes: 73 additions & 0 deletions more-examples/addon-3rdParty-withScope/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<!DOCTYPE html>
<html ng-app="myApp" ng-strict-di ng-cloak="">
<head>
<meta charset="utf-8" />
<title>Angular-Validation Example with Interpolation</title>
<link rel="stylesheet" href="../../style.css">
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="http://mbenford.github.io/ngTagsInput/css/ng-tags-input.min.css" />
<link rel="stylesheet" href="http://isteven.github.io/angular-multi-select/css/isteven-multi-select.css">
</head>

<body ng-controller="Ctrl as vm">
<div class="container">
<form name="test">
<div class="form-group">
<label>
Dropdown MultiSelect
</label>
<div name="select1"
ng-dropdown-multiselect=""
options="select1data"
selected-model="select1model"
ng-model="select1model"
extra-settings="{externalIdProp: ''}"
validation="required" validation-array-objprop="label">
</div>
</div>
<div class="form-group">
<label>
Textbox
</label>
<input class="form-control" type="text" name="nametest" ng-model="myTextbox" validation="required">
</div>
</form>
<hr />
<div class="form-actions">
<button type="submit" name="save_btn" class="btn btn-primary" ng-click="submit()">{{ 'SAVE' | translate }}</button>
</div>
</div>

<!-- external librairies CDN -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-route.js"></script>

<!-- angular-translate -->
<!-- Visit Angular-Translate https://github.com/PascalPrecht/angular-translate -->
<script src="../../vendors/angular-translate/angular-translate.min.js"></script>
<script src="../../vendors/angular-translate/angular-translate-loader-static-files.min.js"></script>

<!-- ngTagsInput -->
<script src="http://mbenford.github.io/ngTagsInput/js/ng-tags-input.min.js"></script>

<!-- AngularJS Dropdown MultiSelect -->
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<script type="text/javascript" src="https://rawgit.com/pc035860/angular-highlightjs/master/angular-highlightjs.js"></script>
<script src="https://rawgit.com/dotansimha/angularjs-dropdown-multiselect/master/src/angularjs-dropdown-multiselect.js"></script>

<!-- AngularJS Multi-Select -->
<script src="https://rawgit.com/isteven/angular-multi-select/master/isteven-multi-select.js"></script>

<!-- Angular-Validation -->
<script type="text/javascript" src="../../dist/angular-validation.min.js"></script>
<!--
<script type="text/javascript" src="../../src/validation-directive.js"></script>
<script type="text/javascript" src="../../src/validation-service.js"></script>
<script type="text/javascript" src="../../src/validation-common.js"></script>
<script type="text/javascript" src="../../src/validation-rules.js"></script>
-->

<!-- my application -->
<script type="text/javascript" src="app.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion more-examples/dynamic-form/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ app.controller('MainCtrl', function($scope,ValidationService) {
// redefine which scope to use inside the Angular-Validation
$scope.$validationOptions = { isolatedScope: $scope };

$scope.validate=function() {
$scope.validate = function() {
for(var key in $scope.items) {
var formName=$scope.items[key].formName;

Expand Down
2 changes: 1 addition & 1 deletion more-examples/dynamic-form/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ <h3>Form Validation (with dynamic form and fields)</h3>
</form>
</tab>
</tabset>
<button name="validateForms" value="Validate" ng-click="validate()">Validate</button>
<button type="submit" name="validateForms" class="btn btn-primary" ng-click="validate()">Validate</button>

<hr/>

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-validation-ghiscoding",
"version": "1.5.10",
"version": "1.5.11",
"author": "Ghislain B.",
"description": "Angular-Validation Directive and Service (ghiscoding)",
"main": "dist/angular-validation.min",
Expand Down
2 changes: 1 addition & 1 deletion protractor/conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
'remote_spec.js',
'mixed_validation_spec.js',
'angularUI_spec.js',
'dynamic_spec.js',
//'dynamic_spec.js',
'controllerAsWithRoute_spec.js',
'interpolate_spec.js',
'ngIfDestroy_spec.js',
Expand Down
3 changes: 3 additions & 0 deletions protractor/dynamic_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
describe('When choosing `more-examples` Dynamic Form Input', function () {
it('Should navigate to Dynamic Form Input home page', function () {
browser.get('http://localhost/github/angular-validation/more-examples/dynamic-form/index.html');
browser.waitForAngular();

// Find the title element
var titleElement = element(by.css('h3'));
Expand All @@ -14,6 +15,8 @@
});

it('Should click on Validate Submit button and expect 2 invalid Forms', function () {
browser.waitForAngular();
browser.sleep(5000);
var validateBtn = $('[name=validateForms]');
validateBtn.click();
browser.waitForAngular();
Expand Down
33 changes: 32 additions & 1 deletion src/validation-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,14 @@ angular
validationCommon.prototype.validate = validate; // validate current element

// override some default String functions
if(window.Element && !Element.prototype.closest) {
Element.prototype.closest = elementPrototypeClosest; // Element Closest Polyfill for the browsers that don't support it (fingers point to IE)
}
String.prototype.trim = stringPrototypeTrim; // extend String object to have a trim function
String.prototype.format = stringPrototypeFormat; // extend String object to have a format function like C#
String.format = stringFormat; // extend String object to have a format function like C#


// return the service object
return validationCommon;

Expand Down Expand Up @@ -800,7 +805,7 @@ angular
}
}

// from the element passed, get his parent form
// from the element passed, get his parent form (this doesn't work with every type of element, for example it doesn't work with <div> or special angular element)
var forms = document.getElementsByName(elmName);
var parentForm = null;

Expand All @@ -822,6 +827,14 @@ angular
}
}

// if we haven't found a form yet, then we have a special angular element, let's try with .closest (this might not work with older browser)
if(!form) {
var element = document.querySelector('[name="'+elmName+'"]');
if(element) {
form = element.closest("form");
}
}

// falling here with a form name but without a form object found in the scope is often due to isolate scope
// we can hack it and define our own form inside this isolate scope, in that way we can still use something like: isolateScope.form1.$validationSummary
if (!!form) {
Expand Down Expand Up @@ -1026,6 +1039,24 @@ angular
return result;
}

/** Element Closest Polyfill for the browsers that don't support it (fingers point to IE)
* https://developer.mozilla.org/en-US/docs/Web/API/Element/closest
*/
function elementPrototypeClosest() {
Element.prototype.closest =
function(s) {
var matches = (this.document || this.ownerDocument).querySelectorAll(s),
i,
el = this;

do {
i = matches.length;
while (--i >= 0 && matches.item(i) !== el) {};
} while ((i < 0) && (el = el.parentElement));
return el;
};
}

/** Override javascript trim() function so that it works accross all browser platforms */
function stringPrototypeTrim() {
return this.replace(/^\s+|\s+$/g, '');
Expand Down

0 comments on commit 032c5ff

Please sign in to comment.