-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from scriptoLLC/validation
Provide hooks for form validation
- Loading branch information
Showing
9 changed files
with
271 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module.exports = function createValidator (errDisplay, validator) { | ||
const key = (new Date() % 9e6).toString(36) | ||
|
||
return function checkValidity (evt) { | ||
const $el = evt.currentTarget | ||
if (typeof validator !== 'function') { | ||
validator = $el.checkValidity.bind($el) | ||
} | ||
|
||
if (!validator($el)) { | ||
$el.dataset.valid = false | ||
return errDisplay.displayError(key, $el.validationMessage) | ||
} | ||
|
||
$el.dataset.valid = true | ||
errDisplay.removeError(key) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = function merge (...funcs) { | ||
return function (...args) { | ||
for (let i = 0, len = funcs.length; i < len; i++) { | ||
funcs[i].call(funcs[i], ...args) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const test = require('tape') | ||
const mergeFuncs = require('../merge-function') | ||
|
||
test('funky merge', (t) => { | ||
t.plan(8) | ||
|
||
function foo (...args) { | ||
t.equal(args.length, 2, '2 args') | ||
t.equal(args[0], 'beep', 'beepin') | ||
t.equal(args[1], 'boop', 'boopin') | ||
t.pass('foo called') | ||
} | ||
|
||
function bar (...args) { | ||
t.equal(args.length, 2, '2 args') | ||
t.equal(args[0], 'beep', 'beepin') | ||
t.equal(args[1], 'boop', 'boopin') | ||
t.pass('bar called') | ||
} | ||
|
||
const merged = mergeFuncs(foo, bar) | ||
merged('beep', 'boop') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
const test = require('tape') | ||
|
||
const error = require('../error-el') | ||
const input = require('../input-el') | ||
|
||
const emailErrors = { | ||
chrome: "Please include an '@' in the email address. 't' is missing an '@'.", | ||
firefox: 'Please enter an email address.', | ||
safari: 'Please enter an email address.' | ||
} | ||
|
||
let browser = 'chrome' | ||
let ua = navigator.userAgent.toLowerCase() | ||
if (ua.includes('firefox')) { | ||
browser = 'firefox' | ||
} else if (ua.includes('chrome')) { | ||
browser = 'chrome' | ||
} else if (ua.includes('safari')) { | ||
browser = 'safari' | ||
} | ||
|
||
test('no validation, no problems', (t) => { | ||
const err = error() | ||
const inputOpts = { | ||
validate: false, | ||
errorDisplay: err | ||
} | ||
const $err = err.render() | ||
const $input = input('email', null, '', inputOpts) | ||
$input.value = 't' | ||
$input.dispatchEvent(new window.Event('blur')) | ||
t.equal($err.children.length, 0, 'no errors') | ||
t.end() | ||
}) | ||
|
||
test('no explicit validation, no problems', (t) => { | ||
const err = error() | ||
const inputOpts = { | ||
errorDisplay: err | ||
} | ||
const $err = err.render() | ||
const $input = input('email', null, '', inputOpts) | ||
$input.value = 't' | ||
$input.dispatchEvent(new window.Event('blur')) | ||
t.equal($err.children.length, 0, 'no errors') | ||
t.end() | ||
}) | ||
|
||
test('built-in validation', (t) => { | ||
t.plan(9) // every time you call blur, the custom onblur runs... | ||
const err = error() | ||
const inputOpts = { | ||
errorDisplay: err, | ||
validate: true, | ||
onblur: () => t.pass('ran custom on blur') | ||
} | ||
const $err = err.render() | ||
const $input = input('email', null, '', inputOpts) | ||
|
||
$input.value = 't' | ||
$input.dispatchEvent(new window.Event('blur')) | ||
t.equal($err.children.length, 1, 'show built-in error') | ||
|
||
$input.dispatchEvent(new window.Event('blur')) | ||
t.equal($err.children.length, 1, 'still only one error') | ||
t.equal($err.children[0].innerText, emailErrors[browser], 'chrome error message') | ||
|
||
$input.value = 't@t' | ||
$input.dispatchEvent(new window.Event('blur')) | ||
t.equal($err.children.length, 0, 'no errors') | ||
|
||
$input.dispatchEvent(new window.Event('blur')) | ||
t.equal($err.children.length, 0, 'still no errors') | ||
}) | ||
|
||
test('custom validation', (t) => { | ||
const err = error() | ||
const inputOpts = { | ||
errorDisplay: err, | ||
validator: ($el) => { | ||
if ($el.value !== 'boop') { | ||
$el.setCustomValidity('You must boop!') | ||
return false | ||
} | ||
$el.setCustomValidity('') | ||
return true | ||
} | ||
} | ||
const $err = err.render() | ||
const $input = input('email', null, '', inputOpts) | ||
|
||
$input.value = 't' | ||
$input.dispatchEvent(new window.Event('blur')) | ||
t.equal($err.children.length, 1, 'only one error') | ||
t.equal($err.children[0].innerText, 'You must boop!', 'custom error message') | ||
|
||
$input.value = 'boop' | ||
$input.dispatchEvent(new window.Event('blur')) | ||
t.equal($err.children.length, 0, 'no errors') | ||
t.end() | ||
}) |