Reconcile Angular.js and Bootstrap form validation styling

前端 未结 12 1951
刺人心
刺人心 2020-12-22 18:53

I am using Angular with Bootstrap. Here is the code for reference:

相关标签:
12条回答
  • 2020-12-22 19:49

    Minor improvement to @farincz's answer. I agree that a directive is the best approach here but I didn't want to have to repeat it on every .form-group element so I updated the code to allow adding it to either the .form-group or to the parent <form> element (which will add it to all contained .form-group elements):

    angular.module('directives', [])
      .directive('showValidation', [function() {
        return {
            restrict: "A",
            link: function(scope, element, attrs, ctrl) {
    
                if (element.get(0).nodeName.toLowerCase() === 'form') {
                    element.find('.form-group').each(function(i, formGroup) {
                        showValidation(angular.element(formGroup));
                    });
                } else {
                    showValidation(element);
                }
    
                function showValidation(formGroupEl) {
                    var input = formGroupEl.find('input[ng-model],textarea[ng-model]');
                    if (input.length > 0) {
                        scope.$watch(function() {
                            return input.hasClass('ng-invalid');
                        }, function(isInvalid) {
                            formGroupEl.toggleClass('has-error', isInvalid);
                        });
                    }
                }
            }
        };
    }]);
    
    0 讨论(0)
  • 2020-12-22 19:50
    <div class="form-group has-feedback" ng-class="{ 'has-error': form.uemail.$invalid && form.uemail.$dirty }">
      <label class="control-label col-sm-2" for="email">Email</label>
      <div class="col-sm-10">
        <input type="email" class="form-control" ng-model="user.email" name="uemail" placeholder="Enter email" required>
        <div ng-show="form.$submitted || form.uphone.$touched" ng-class="{ 'has-success': form.uemail.$valid && form.uemail.$dirty }">
        <span ng-show="form.uemail.$valid" class="glyphicon glyphicon-ok-sign form-control-feedback" aria-hidden="true"></span>
        <span ng-show="form.uemail.$invalid && form.uemail.$dirty" class="glyphicon glyphicon-remove-circle form-control-feedback" aria-hidden="true"></span>
        </div>
      </div>
    </div>
    
    0 讨论(0)
  • 2020-12-22 19:52

    Use Bootstrap's "error" class for styling. You can write less code.

    <form name="myForm">
      <div class="control-group" ng-class="{error: myForm.name.$invalid}">
        <label>Name</label>
        <input type="text" name="name" ng-model="project.name" required>
        <span ng-show="myForm.name.$error.required" class="help-inline">
            Required</span>
      </div>
    </form>
    

    EDIT: As other answers and comments point out - in Bootstrap 3 the class is now "has-error", not "error".

    0 讨论(0)
  • 2020-12-22 19:53

    If styling is the issue, but you don't want to disable the native validation, why not override the styling with your own, more specific style?

    input.ng-invalid, input.ng-invalid:invalid {
       background: red;
       /*override any styling giving you fits here*/
    }
    

    Cascade your problems away with CSS selector specificity!

    0 讨论(0)
  • 2020-12-22 19:56

    The classes have changed in Bootstrap 3:

    <form class="form-horizontal" name="form" novalidate ng-submit="submit()" action="/login" method="post">
      <div class="row" ng-class="{'has-error': form.email.$invalid, 'has-success': !form.email.$invalid}">
        <label for="email" class="control-label">email:</label>
        <div class="col">
        <input type="email" id="email" placeholder="email" name="email" ng-model="email" required>
        <p class="help-block error" ng-show="form.email.$dirty && form.email.$error.required">please enter your email</p>
        <p class="help-block error" ng-show="form.email.$error.email">please enter a valid email</p>
      ...
    

    Note the quotes around 'has-error' and 'has-success': took a while to find that...

    0 讨论(0)
  • 2020-12-22 19:57

    Minor improvement to @Andrew Smith's answer. I change input elements and using require keyword.

    .directive('showValidation', [function() {
        return {
            restrict: "A",
            require:'form',
            link: function(scope, element, attrs, formCtrl) {
                element.find('.form-group').each(function() {
                    var $formGroup=$(this);
                    var $inputs = $formGroup.find('input[ng-model],textarea[ng-model],select[ng-model]');
    
                    if ($inputs.length > 0) {
                        $inputs.each(function() {
                            var $input=$(this);
                            scope.$watch(function() {
                                return $input.hasClass('ng-invalid');
                            }, function(isInvalid) {
                                $formGroup.toggleClass('has-error', isInvalid);
                            });
                        });
                    }
                });
            }
        };
    }]);
    
    0 讨论(0)
提交回复
热议问题