-
Notifications
You must be signed in to change notification settings - Fork 102
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
change wrapper element assignment #79
Open
MichalRemis
wants to merge
3
commits into
DavyJonesLocker:main
Choose a base branch
from
MichalRemis:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
125 changes: 125 additions & 0 deletions
125
test/javascript/public/test/form_builders/validateHorizontalSimpleFormBootstrap4.js
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,125 @@ | ||
QUnit.module('Validate Horizontal wrapper SimpleForm Bootstrap 4', { | ||
before: function () { | ||
currentFormBuilder = window.ClientSideValidations.formBuilders['SimpleForm::FormBuilder'] | ||
window.ClientSideValidations.formBuilders['SimpleForm::FormBuilder'] = BS4_FORM_BUILDER | ||
}, | ||
|
||
after: function () { | ||
window.ClientSideValidations.formBuilders['SimpleForm::FormBuilder'] = currentFormBuilder | ||
}, | ||
|
||
beforeEach: function () { | ||
dataCsv = { | ||
html_settings: { | ||
type: 'SimpleForm::FormBuilder', | ||
error_class: 'is-invalid', | ||
error_tag: 'div', | ||
wrapper_error_class: 'form-group-invalid', | ||
wrapper_tag: 'div', | ||
wrapper_class: 'form-group' | ||
}, | ||
validators: { | ||
'user[name]': { presence: [{ message: 'must be present' }], format: [{ message: 'is invalid', 'with': { options: 'g', source: '\\d+' } }] }, | ||
'user[username]': { presence: [{ message: 'must be present' }] } | ||
} | ||
} | ||
|
||
$('#qunit-fixture') | ||
.append( | ||
$('<form>', { | ||
action: '/users', | ||
'data-client-side-validations': JSON.stringify(dataCsv), | ||
method: 'post', | ||
id: 'new_user' | ||
}) | ||
.append( | ||
$('<div>', { 'class': 'form-group row' }) | ||
.append( | ||
$('<label for="user_name" class="string col-sm-3 col-form-label">Name</label>')) | ||
.append( | ||
$('<div>', { 'class': 'col-sm-9' }) | ||
.append( | ||
$('<input />', { 'class': 'form-control', name: 'user[name]', id: 'user_name', type: 'text' })))) | ||
// there isn't horizontal :input_group wrapper in simple_form's bootstrap 4 configuration by default | ||
// but if somebody would do it it would look like this | ||
.append( | ||
$('<div>', { 'class': 'form-group row' }) | ||
.append( | ||
$('<label for="user_username" class="string col-sm-3 col-form-label">Username</label>')) | ||
.append( | ||
$('<div>', { 'class': 'col-sm-9' }) | ||
.append( | ||
$('<div>', { 'class': 'input-group' }) | ||
.append( | ||
$('<div>', { 'class': 'input-group-prepend' }) | ||
.append( | ||
$('<span>', { 'class': 'input-group-text', text: '@' }))) | ||
.append( | ||
$('<input />', { 'class': 'form-control', name: 'user[username]', id: 'user_username', type: 'text' })))))) | ||
|
||
$('form#new_user').validate() | ||
} | ||
}) | ||
|
||
var wrappers = ['horizontal_form' ] | ||
|
||
for (var i = 0; i < wrappers.length; i++) { | ||
var wrapper = wrappers[i] | ||
|
||
QUnit.test(wrapper + ' - Validate error attaching and detaching', function (assert) { | ||
var form = $('form#new_user') | ||
var input = form.find('input#user_name') | ||
var label = $('label[for="user_name"]') | ||
form[0].ClientSideValidations.settings.html_settings.wrapper = wrapper | ||
|
||
input.trigger('focusout') | ||
assert.ok(input.closest('.form-group').hasClass('form-group-invalid')) | ||
assert.ok(label.parent().hasClass('form-group-invalid')) | ||
assert.ok(input.parent().find('div.invalid-feedback:contains("must be present")')[0]) | ||
|
||
input.val('abc') | ||
input.trigger('change') | ||
input.trigger('focusout') | ||
assert.ok(input.closest('.form-group').hasClass('form-group-invalid')) | ||
assert.ok(input.parent().find('div.invalid-feedback:contains("is invalid")')[0]) | ||
assert.ok(input.hasClass('is-invalid')) | ||
|
||
input.val('123') | ||
input.trigger('change') | ||
input.trigger('focusout') | ||
assert.notOk(input.closest('.form-group').parent().hasClass('form-group-invalid')) | ||
assert.notOk(input.parent().parent().find('div.invalid-feedback:contains("is invalid")')[0]) | ||
assert.notOk(input.hasClass('is-invalid')) | ||
}) | ||
|
||
QUnit.test(wrapper + ' - Validate pre-existing error blocks are re-used', function (assert) { | ||
var form = $('form#new_user'); var input = form.find('input#user_name') | ||
var label = $('label[for="user_name"]') | ||
form[0].ClientSideValidations.settings.html_settings.wrapper = wrapper | ||
|
||
input.parent().append($('<div class="invalid-feedback">Error from Server</span>')) | ||
assert.ok(input.parent().find('div.invalid-feedback:contains("Error from Server")')[0]) | ||
input.val('abc') | ||
input.trigger('change') | ||
input.trigger('focusout') | ||
assert.ok(input.closest('.form-group').hasClass('form-group-invalid')) | ||
assert.ok(label.parent().hasClass('form-group-invalid')) | ||
assert.ok(input.parent().find('div.invalid-feedback:contains("is invalid")').length === 1) | ||
assert.ok(form.find('div.invalid-feedback').length === 1) | ||
}) | ||
|
||
QUnit.test(wrapper + ' - Validate input-group', function (assert) { | ||
var form = $('form#new_user'); var input = form.find('input#user_username') | ||
form[0].ClientSideValidations.settings.html_settings.wrapper = wrapper | ||
|
||
input.trigger('change') | ||
input.trigger('focusout') | ||
assert.ok(input.closest('.input-group-prepend').find('div.invalid-feedback').length === 0) | ||
assert.ok(input.closest('.input-group').find('div.invalid-feedback').length === 1) | ||
|
||
input.val('abc') | ||
input.trigger('change') | ||
input.trigger('focusout') | ||
assert.ok(input.closest('.input-group').find('div.invalid-feedback').length === 0) | ||
}) | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Calling
find
on a String, this will fail. The comment usedelement.closest
that is missing hereThere 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.
Yes, sorry may fault, I made a mistake while copy&pasting. Downloaded the repo locally now and made sure that test are passing in next commit. Also I think e.g. validateSimpleFormBootstrap4.js test for horizontal_form SimpleForm's bootstrap wrapper isn't absolutely correct, because current SimpleForm's bootstrap configuration would make HTML for field with error like this:
So the
form-group-invalid
class isn't on parent of the input but on the wrapper and that's what I am trying to address with my PR.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.
Thanks!
Feel free to fix the test
Anyway, In this case, I think that the error should be added to the parent element. Maybe we can add a check on the form's css class and provide two different approaches
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.
Ok I will fix the test but then the current
element.parent()
method of addingwrapper_error_class
would be failing with :horizontal_form wrapper. I just thought newelement.closest('.form-group')
method is more universal for current simple_form's bootstrap4 configuration working with most of it's wrappers.I don't think there's wrapper's class on form's css, because wrapper is a simple_form's thing, not bootstrap's. If different approaches are needed (and I think it will be for more complex wrappers), CSV has a nice way to do this by implementing wrapper's specific add/remove methods in
ClientSideValidations.formBuilders['SimpleForm::FormBuilder']
e.g.: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.
Ok, I would go for the closest form group, but it will not work for any use case.
Probably I went for
element.parent
instead of the closest wrapper that I've suggested because at the time I wrote this solution it was the choice that worked with most of the combinationsThere 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.
Ok I can see what you mean. The error message (.invalid-feedback) should be added to parent element, in that case element.parent() is right. But when adding wrapper_error_class, wrapper tag isn't always the parent. Maybe we should split this.. add error to parentElement and add wrapper_error_class to wrapperElement.
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.
I will fix/add tests for horizontal_wrapper, vertical_wrappers and inline_wrapper according to current simple_form default bootstrap config and we may test it against those, what do you think?
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.
Yes, it is mostly correct
wrapper_errror_class
should not be needed at all by Bootstrap 4 itself, we could remove thatI can see that all examples provided here: https://getbootstrap.com/docs/4.4/components/forms/ (search for
invalid-feedback
) are attached to the parent element (actually the sibling of the input element, but it should not be the case ofinput-group-append
).If you have a comprehensive page with a lot of form elements generated by the latest default bootstrap+simple form, please share the html here (or in a gist), so I can also take a look
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.
You can find latest simple_form examples at http://simple-form-bootstrap.plataformatec.com.br/ (submit forms for invalid markup). You are right most of wrappers add
invalid-feedback
as sibling of the input, except for Input Group Form (but yes, bootstrap4 docs has it as sibling so maybe it works both ways. This also cause double invalid-feedback when (not) re-using existing server error block.) and special date/radio/etc.. inputs which CSV doesn't support anyway.Anyway purpose of my PR was to fix
wrapper_errror_class
assignment which ATM doesn't work for Horizontal Form and Input Group Form examples. You are rightwrapper_errror_class
is not needed by bootstrap but it's SimpleForm's thing and people may expect it there and could use it e.g. for styling of label. Sure it's possible to customize JS formBuilder but it would be nice if it works by default.I committed JS tests for :horizontal_form wrapper and made them pass with a change which I believe also solves double invalid-feedback for input-group fields.