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

(v4) Form validation #22192

Closed
tm4321 opened this issue Mar 15, 2017 · 1 comment
Closed

(v4) Form validation #22192

tm4321 opened this issue Mar 15, 2017 · 1 comment
Labels
Milestone

Comments

@tm4321
Copy link

tm4321 commented Mar 15, 2017

I love how easy it is to style forms with bootstrap but I felt that the validation classes could be improved a bit. For those of you who want to jump straight to the demo, here you go.

Lets look at the first validation example in the docs.

Currently we would have to do the following steps in javascript to toggle between the different validation states:

  1. Remove the previous .has-* from the parent element and then apply the new class
  2. Remove the previous .form-control-* from the input and then apply the new class
  3. Overwrite the inner HTML of the .form-control-feedback with the new message

I think that we could eliminate step 2 and 3 by making the following changes:

  • collapse .form-control-* into a single class that changes its background-image based upon the .has-* of its parent
  • set .form-control-feedback content to data attributes based upon the .has-* of its parent

_forms.scss:

.form-control-feedback {
  margin-top: $form-feedback-margin-top;

  &:after {
    display: block;
    content: "";
    opacity: 0;
  }
}

.has-success,
.has-warning,
.has-danger {
  .form-control.feedback-icon {
    padding-right: ($input-padding-x * 3);
    background-repeat: no-repeat;
    background-position: center right ($input-height / 4);
    background-size: ($input-height / 2) ($input-height / 2);
  }
}

// Form validation states
.has-success {
  @include form-control-validation($brand-success);

  .form-control.feedback-icon {
    background-image: $form-icon-success;
  }

  .form-control-feedback:after {
    content: attr(data-success);
    opacity: 1;
  }
}

.has-warning {
  @include form-control-validation($brand-warning);

  .form-control.feedback-icon {
    background-image: $form-icon-warning;
  }

  .form-control-feedback:after {
    content: attr(data-warning);
    opacity: 1;
  }
}

.has-danger {
  @include form-control-validation($brand-danger);

  .form-control.feedback-icon {
    background-image: $form-icon-danger;
  }

  .form-control-feedback:after {
    content: attr(data-danger);
    opacity: 1;
  }
}

I also added two new transitions to be in sync with .form-control

_variables.scss:

$input-group-addon-transition   : background-color ease-in-out .15s, border-color ease-in-out .15s !default; 
$form-feedback-transition       : opacity ease-in-out .15s, color ease-in-out .15s !default;

_forms.scss:

.form-control-feedback {
  &:after {
    @include transition($form-feedback-transition);
  }
}

_input-group.scss:

.input-group-addon {
  @include transition($input-group-addon-transition);
}

This is probably outside the scope of bootstrap but I also wrote some javascript that leverages HTML5 validation attributes to apply the appropriate .has-*. I plan on expanding this to include a pattern for .has-warning but this jsbin gets the general idea across.

@tm4321 tm4321 changed the title Form validation (v4) Form validation Mar 15, 2017
tm4321 added a commit to tm4321/bootstrap that referenced this issue Apr 4, 2017
 - collapse .form-control-* into .feedback-icon
 - add :after to .form-control-feedback with content set by data-attributes based on .has-* of parent
 - add transitions to .input-group-addon and .feedback-icon to be in sync with .form-control
@mdo mdo modified the milestone: v4.0.0-beta Apr 22, 2017
tm4321 added a commit to tm4321/bootstrap that referenced this issue May 12, 2017
     - collapse .form-control-* into .feedback-icon
     - add :after to .form-control-feedback with content set by data-attributes based on .has-* of parent
     - add transitions to .input-group-addon and .feedback-icon to be in sync with .form-control
@mdo
Copy link
Member

mdo commented Jun 7, 2017

Revisiting validation states in #22762.

@mdo mdo closed this as completed Jun 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants