Skip to content

Commit 2737b7d

Browse files
authored
Merge pull request #25 from JenkinsDev/hotfix/check_for_dom_objects_inside_forms
Hotfix: Check for dom objects inside forms
2 parents 5b3739d + a449330 commit 2737b7d

12 files changed

+206
-178
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
language: node_js
22
node_js:
3-
- "0.10"
3+
- "6"

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module.exports = function(grunt) {
55
uglify: {
66
options: {
77
banner: '/* \n' +
8-
' * Copyright (c) 2013-2016 David Jenkins (<%= pkg.name %>) \n' +
8+
' * Copyright (c) 2013-2017 David Jenkins (<%= pkg.name %>) \n' +
99
' * See the file license.txt for copying permission. \n' +
1010
' * \n' +
1111
' * Simple, yet effective, vanilla JavaScript form validation "plugin." Validatinator is based off \n' +

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Validatinator
22

3-
Current Release: 1.3.3
3+
Current Release: 1.3.4
44

55
Validatinator is a simple, yet effective, vanilla JavaScript form validation
66
"plugin." It is loosely based off of Laravel's validation system. Using

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "validatinator",
3-
"version": "1.3.3",
3+
"version": "1.3.4",
44
"homepage": "https://github.com/JenkinsDev/Validatinator",
55
"authors": [
66
"David Jenkins <david.nicholas.jenkins@gmail.com>"

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Validatinator Changelog
22

3+
### Version 1.3.4
4+
5+
* Hotfix: Validatinator.utils.getFieldsValue() should now correctly only return a field's value if it is truly a field within the form requested.
6+
* Hotfix [tests]: Added a bit of cleanup after DOM manipulation is done in tests.
7+
38
### Version 1.3.3
49

510
* Hotfix: Updated email regex to be less strict and follow RFC822.

dev/js/validatinator.core.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,13 @@ Validatinator.prototype = {
7373
i;
7474

7575
this.currentForm = formName;
76-
// Since we are doing a fresh validation let's make sure our errors are all fresh as well!
7776
this.errors = {};
7877

7978
for (var fieldName in this.validationInformation[formName]) {
8079
this.currentField = fieldName;
8180
currentFieldsValidations = this.validationInformation[formName][fieldName];
8281
currentFieldsValue = this.utils.getFieldsValue(this.currentForm, this.currentField);
8382

84-
// We need to set i here because it doesn't reset to zero by default and it is more idomatic to do it here.
8583
for (i = 0; i < currentFieldsValidations.length; i++) {
8684
var method,
8785
parameters = [];

dev/js/validatinator.utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ Validatinator.prototype.utils = {
160160

161161
// We are running a simple test to see if the current field in the returned array is part of
162162
// our validating field or not. If it is then grab it's value and break out of this test loop.
163-
if (fieldElement.form.name === form) {
164-
if((fieldElement.type == 'radio' || fieldElement.type == 'checkbox') && !fieldValue) {
163+
if (fieldElement.form && fieldElement.form.name === form) {
164+
if (['radio', 'checkbox'].indexOf(fieldElement.type) !== -1 && !fieldValue) {
165165
if(fieldElement.checked) {
166166
fieldValue = fieldElement.value;
167167
break;

dev/tests/validatinator_utils_test.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe("Validatinator Utils", function() {
6464
expect(utils.isValueAnArray("not an array")).toBeFalsy();
6565
});
6666

67-
describe('Set Test Form For Value Retrieval', function() {
67+
describe('getFieldsValue', function() {
6868
beforeEach(function() {
6969
// Creating our testing form.
7070
var myForm = document.createElement('form'),
@@ -76,16 +76,19 @@ describe("Validatinator Utils", function() {
7676
myForm.name = "my-form";
7777
firstName.name = "first-name";
7878
lastName.name = "last-name";
79+
7980
gender.name = "gender";
8081
gender.type = "radio";
8182
gender.value = "male";
83+
8284
languages.name = "languages";
8385
languages.type = "checkbox";
8486
languages.value = "english";
8587

8688
lastName.value = "test";
8789

8890
document.body.appendChild(myForm);
91+
8992
// Now that our element is in the dom let's select it again.
9093
myForm = document.getElementsByName("my-form")[0];
9194

@@ -96,21 +99,43 @@ describe("Validatinator Utils", function() {
9699
myForm.appendChild(languages);
97100
});
98101

99-
it('getFieldsValue should throw an error if there is no field to grab a value from, else return the field\'s value.', function() {
102+
// Do proper cleanup after each of these tests.
103+
afterEach(function() {
104+
document.body.removeChild(document.querySelector('[name="my-form"]'));
105+
});
106+
107+
it('should throw an error if the field doesn\'t exist.', function() {
100108
expect(utils.getFieldsValue("my-form", "first-name")).toEqual("");
101109
expect(utils.getFieldsValue("my-form", "last-name")).toEqual("test");
102110

103-
expect(function() { utils.getFieldsValue("my-form", "some-fake-field"); }).toThrow("Couldn't find the field element some-fake-field for the form my-form.");
111+
expect(function() {
112+
utils.getFieldsValue("my-form", "some-fake-field");
113+
}).toThrow();
104114
});
105115

106-
it('getFieldsValue should only return the value from radio or checkbox if its actually selected/checked', function(){
116+
it('should only return a value for radio or checkbox if its actually selected.', function() {
107117
expect(utils.getFieldsValue("my-form", "gender")).toEqual("");
108118
expect(utils.getFieldsValue("my-form", "languages")).toEqual("");
109119

110120
document.getElementsByName("gender")[0].checked = true;
111121
document.getElementsByName("languages")[0].checked = true;
122+
112123
expect(utils.getFieldsValue("my-form", "gender")).toEqual("male");
113124
expect(utils.getFieldsValue("my-form", "languages")).toEqual("english");
114125
});
126+
127+
it('should only evaluate field DOM objects that are within our form', function() {
128+
// Test specific setup
129+
var fieldName = "first-name";
130+
131+
var meta = document.createElement('meta');
132+
meta.name = fieldName;
133+
meta.value = "David";
134+
135+
var head = document.querySelector("head");
136+
head.appendChild(meta);
137+
138+
expect(utils.getFieldsValue("my-form", fieldName)).toEqual("");
139+
});
115140
});
116-
});
141+
});

0 commit comments

Comments
 (0)