Skip to content
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

Override default validation state handing - Bootstrap 4 #1212

Closed
1 of 3 tasks
fran-worley opened this issue Jul 1, 2017 · 7 comments
Closed
1 of 3 tasks

Override default validation state handing - Bootstrap 4 #1212

fran-worley opened this issue Jul 1, 2017 · 7 comments

Comments

@fran-worley
Copy link

fran-worley commented Jul 1, 2017

What kind of issue is this? (put 'x' between the square brackets)

In Bootstrap4, you need to add the error class to both the input and its wrapping '.form-group' (errorsContainer). By default in ParsleyJS you can only define one error class handler and error/ success class.

I've hacked this for my purposes by listening to the form:validate event and injecting the additional classes but it causes issues when destroying/ resetting a parsley form as I'm unable to cleanup the additional states nicely.

Ideally, I'd like to be able to pass my own error/ success/ reset class functions into parsley to override these:

_successClass: function _successClass() {
      this._ui.validationInformationVisible = true;
      this._ui.$errorClassHandler.removeClass(this.options.errorClass).addClass(this.options.successClass);
},
_errorClass: function _errorClass() {
      this._ui.validationInformationVisible = true;
      this._ui.$errorClassHandler.removeClass(this.options.successClass).addClass(this.options.errorClass);
},
_resetClass: function _resetClass() {
      this._ui.$errorClassHandler.removeClass(this.options.successClass).removeClass(this.options.errorClass);
}
@marcandre
Copy link
Collaborator

Thanks for opening this request.
Indeed, it would be nice to be able to use bootstrap's convention easily.
Probably the easiest feature to add would be some options for the input's error & success classes. Care to provide a PR for this?

@fran-worley
Copy link
Author

The only complication is that in BS4 you don't add the input class to all fields. The input class adds a feedback icon (e.g. tick, cross, warning exclamation mark), and is only designed to work with regular inputs (text, email, number, password etc.).

Hence why I thought it might be easier to just allow customisation of the actual functions that apply validation states.

Happy to help with a PR if we can agree on a suitable plan of implementation.

@marcandre
Copy link
Collaborator

I see. If you add the class to other inputs, does it create problems though?

@fran-worley
Copy link
Author

It definitely messes up select boxes, particularly their custom selects. Text areas seem ok and it appears to ignore additional classes on checkboxes and radios.

@marcandre
Copy link
Collaborator

Ok, so I agree with you that it needs to be the developper's responsibility, but that it should be easy. There should be an example of wiki page with it.
The solution should look like:

var bs4Setter = function(isSuccess, isError) {
  return function(p) {
    if(p.$element.is('input[type!="select"]')) {
      p.$element
        .toggleClass('form-control-success', isSuccess)
        .toggleClass('form-control-danger', isError)
    }
  }
}
Parsley
  .on('field:error', bs4Setter(false, true))
  .on('field:success', bs4Setter(true, false))
  .on('field:reset', bs4Setter(false, false))

(not tested)

If I understand correctly, the above sort of works, but doesn't quite for the case of being reset, right?
If so, the solution would be to insure that field:reset is called whenever the success/failure class is removed, right?

@fran-worley
Copy link
Author

I think this can be closed (unless you want to add it for bootstrap3 or others). As of Bootstrap 4.0.0.beta1 they have simplified validation classes:
https://getbootstrap.com/docs/4.0/components/forms/#validation

They now just require a state/ class on the element rather than the wrapper too.

@marcandre
Copy link
Collaborator

Closing then, if someone as a clear proposal, don't hesitate to reopen or open a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants