【问题标题】:Bootstrap: Validation states with form-inline fields?Bootstrap:带有表单内联字段的验证状态?
【发布时间】:2013-05-21 22:01:01
【问题描述】:

有什么方法可以在 form-inline 字段上使用 Bootstrap 的表单字段验证状态/类(warningerrorinfosuccess)(不使用control-group)?


我正在使用 Bootstrap,并且我有一个使用 form-horizontal 类进行布局的大型表单。但是,表单中的某些区域需要内联字段(例如,City/State/Zip)。我为此使用form-inline,效果很好。这是基本标记:

<form id="account-form" class="form-horizontal" method="post" action="" novalidate>
    <!-- Address -->
    <div class="control-group">
        <label class="control-label">Address</label>

        <div class="controls">
            <input type="text" name="address" />
        </div>
    </div>

    <!-- City, State, Zip -->
    <div class="control-group">
        <label class="control-label">City</label>

        <div class="controls form-inline">
            <input type="text" name="city" />

            <label>State</label>
            <input type="text" name="state" />

            <label>Zip Code</label>
            <input type="text" name="zipcode" />
        </div>
    </div>
</form>

在我的特殊情况下,我需要手动处理验证。不幸的是,Bootstrap 的验证状态类似乎必须应用于control-group;正如您在上面的示例中看到的,在我的例子中,control-group 包含多个字段。我尝试将单个字段包装在 control-group 跨度或 div 中,但 control-groupform-inlineform-horizontal 一起使用时效果不佳!

是否有一个 CSS 类或其他规则我可以​​直接附加到一个字段(或其他无害的字段包装器)以将这些样式应用于各个字段,而无需完全重新声明所有标准引导程序样式??

【问题讨论】:

标签: validation twitter-bootstrap


【解决方案1】:

您可以创建一些自定义的更少代码并重新编译引导程序:

@import "_custom.less"; 添加到less/bootstrap.less

创建less/_custom.less

// Mixin for inline form field states
.inlineformFieldState(@warning: success,@textColor: #555, @borderColor: #ccc, @backgroundColor: #f5f5f5) {
  // Set the text color
  label.@{warning},
  .help-block .@{warning},
  .help-inline .@{warning}{
    color: @textColor;
  }
  // Style inputs accordingly
  .checkbox .@{warning},
  .radio .@{warning},
  input.@{warning},
  select.@{warning},
  textarea.@{warning} {
    color: @textColor;
  }
  input.@{warning},
  select.@{warning},
  textarea.@{warning} {
    border-color: @borderColor;
    .box-shadow(inset 0 1px 1px rgba(0,0,0,.075)); // Redeclare so transitions work
    &:focus {
      border-color: darken(@borderColor, 10%);
      @shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 6px lighten(@borderColor, 20%);
      .box-shadow(@shadow);
    }
  }
  // Give a small background color for input-prepend/-append
  .input-prepend .add-on .@{warning},
  .input-append .add-on .@{warning} {
    color: @textColor;
    background-color: @backgroundColor;
    border-color: @textColor;
  }
}

.form-inline{
  .inlineformFieldState(success, @successText, @successText, @successBackground);
  .inlineformFieldState(warning, @warningText, @warningText, @warningBackground);
  .inlineformFieldState(info, @infoText, @infoText, @infoBackground);
  .inlineformFieldState(error, @errorText, @errorText, @errorBackground);
}

这个 mixin 基于less/mixins.less 中的.formFieldState。重新编译引导程序后,您可以使用(另请参阅:http://jsfiddle.net/bassjobsen/K3RE3/):

<form>
<div class="container">
   <div class="control-group">
        <div class="controls form-inline">
            <label class="error">City</label>
            <input class="error" type="text" name="city" />
            <label class="warning">State</label>
            <input class="warning" type="text" name="state" />
            <label class="success">Zip Code</label>
            <input class="success" type="text" name="zipcode" />
            <label class="info">Street</label>
            <input class="info" type="text" name="street" />
        </div>
</div>
</div>
</form>   

【讨论】:

    【解决方案2】:

    我这样做,从 div 容器中删除 control-group 并改用 form-group,但是将它放在每个输入 div 中,我不确保与 control-group 一起使用。 这个网址也许可以帮助你http://formvalidation.io/examples/complex-form/,这对我不起作用^_^!。希望对你有所帮助。

    <div class="row">
        <div class="col-lg-12">
            <div class="col-lg-4 form-group" >
                <select class="form-control" name="pais">
                    <option value="" selected disabled>Pais</option>
                    <option value="a">A</option>
                    <option value="b">B</option>
                    <option value="c">C</option>
                </select>
            </div>
            <div class="col-lg-4 form-group" >
                <select class="form-control" name="estado">
                    <option value="" selected disabled>Estado</option>
                    <option value="a">A</option>
                    <option value="b">B</option>
                    <option value="">C</option>
                </select>
            </div>
            <div class="col-lg-4 form-group">
                <select class="form-control"  name="ciudad">
                    <option value="" selected disabled>Ciudad</option>
                    <option value="a">A</option>
                    <option value="b">B</option>
                    <option value="c">C</option>
                </select>
            </div>
        </div>
    </div>
    

    【讨论】:

    • 这不是用户要问的。
    【解决方案3】:

    这样的事情会起作用。

    <script>
    // Example starter JavaScript for disabling form submissions if there are invalid fields
    (function() {
      'use strict';
      window.addEventListener('load', function() {
        // Fetch all the forms we want to apply custom Bootstrap validation styles to
        var forms = document.getElementsByClassName('needs-validation');
        // Loop over them and prevent submission
        var validation = Array.prototype.filter.call(forms, function(form) {
          form.addEventListener('submit', function(event) {
            if (form.checkValidity() === false) {
              event.preventDefault();
              event.stopPropagation();
            }
            form.classList.add('was-validated');
    
            //******** added custom code.
            $('input[type="radio"]').closest('.col-sm-9').find('.invalid-feedback').hide();
            $('input[type="radio"]:invalid').closest('.col-sm-9').find('.invalid-feedback').show();
          }, false);
            //******** added custom code.
        });
      }, false);
    })();
    </script>
    

    上面的代码几乎是文档,在提交事件监听器的末尾添加了自定义代码。

    @读 https://getbootstrap.com/docs/4.0/components/forms/#validation

    【讨论】:

      猜你喜欢
      • 2016-01-01
      • 2015-08-13
      • 1970-01-01
      • 2011-09-17
      • 2018-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多