New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(Abide): add validator-specific error messages #11698
feat(Abide): add validator-specific error messages #11698
Conversation
Hi @soylent, awesome PR which also includes new tests. We'll check it and give you feedback. |
Thanks, @DanielRuf. Please let me know if I can make this PR better. |
Given the data-attribute API is the most common approach, I think the backwards incompatibility in the JS API is probably fine, but we'll need to note it in release notes. There is a merge conflict right now - I can address that when merging. @DanielRuf do you see any reason not to merge this for 6.6? |
We could also need unit tests but so far it looks good. |
js/foundation.abide.js
Outdated
@@ -132,6 +133,15 @@ class Abide extends Plugin { | |||
|
|||
$error = $error.add(this.$element.find(`[data-form-error-for="${id}"]`)); | |||
|
|||
if (failedValidators) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (failedValidators) { | |
if (!!failedValidators) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
@@ -417,7 +431,7 @@ class Abide extends Plugin { | |||
// A pattern can be passed to this function, or it will be infered from the input's "pattern" attribute, or it's "type" attribute | |||
pattern = (pattern || $el.attr('pattern') || $el.attr('type')); | |||
var inputText = $el.val(); | |||
var valid = false; | |||
var valid = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be unrelated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see my other comment
@@ -428,13 +442,6 @@ class Abide extends Plugin { | |||
else if (pattern !== $el.attr('type')) { | |||
valid = new RegExp(pattern).test(inputText); | |||
} | |||
else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be unrelated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The validateText()
method was performing two validations:
- pattern validation
- checked if the input is required
It returns false if either of the two validations fails, but to implement this feature, I need to distinguish between those two cases, so I removed the second validation (the requiredCheck()
method should be used instead). Please see:
https://github.com/foundation/foundation-sites/pull/11698/files#diff-37998d120faf57b3e7580109c8e306e2R346-R347
I think this is a reasonable change because:
- the method now does only one thing instead of two
- the documentation now matches the implementation
@@ -0,0 +1,120 @@ | |||
<!doctype html> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, tests =)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my feedback.
3034a3f
to
4cd93b3
Compare
Please resolve the conflicts / rebase. |
Now you can add different error messages for each validator using the `[data-form-error-on]` attribute. <input required type="email"> <span class="form-error" data-form-error-on="required">Required</span> <span class="form-error" data-form-error-on="pattern">Invalid</span> BREAKING CHANGE: `Foundation.Abide#validateText()` no longer checks if the input is required. Use the `Foundation.Abide#requiredCheck()` method for that. Closes foundation#10799
4cd93b3
to
5eac681
Compare
Done! |
This pull request has been mentioned on Foundation Open Source Community. There might be relevant details there: https://foundation.discourse.group/t/foundation-for-sites-6-6-2/1936/1 |
Description
It would be great to display different error messages depending on failed validation.
Now you can add different error messages for each validator using the
[data-form-error-on]
attribute:BREAKING CHANGE:
Foundation.Abide#validateText()
no longer checks if the input is required. Use theFoundation.Abide#requiredCheck()
method for that.Types of changes
Checklist
develop
ordevelop-v...
).