show validation error messages on submit in angularjs

前端 未结 13 766
别那么骄傲
别那么骄傲 2020-11-28 02:25

I have a form which need to show validation error messages if clicked submit.

Here is a working plunker

 
相关标签:
13条回答
  • 2020-11-28 03:00

    I like the solution from realcrowd the best.

    HTML:

    <form role="form" id="form" name="form" autocomplete="off" novalidate rc-submit="signup()">
    <div class="form-group" ng-class="{'has-error':  rc.form.hasError(form.firstName)}">
        <label for="firstName">Your First Name</label>
        <input type="text" id="firstName" name="firstName" class="form-control input-sm" placeholder="First Name" ng-maxlength="40" required="required" ng-model="owner.name.first"/>
        <div class="help-block" ng-show="rc.form.hasError(form.firstName)">{{rc.form.getErrMsg(form.firstName)}}</div>
    </div>
    </form>
    

    javascript:

    //define custom submit directive
    var rcSubmitDirective = {
    'rcSubmit': ['$parse', function ($parse) {
        return {
            restrict: 'A',
            require: ['rcSubmit', '?form'],
            controller: ['$scope', function ($scope) {
                this.attempted = false;
    
                var formController = null;
    
                this.setAttempted = function() {
                    this.attempted = true;
                };
    
                this.setFormController = function(controller) {
                  formController = controller;
                };
    
                this.hasError = function (fieldModelController) {
                    if (!formController) return false;
    
                    if (fieldModelController) {
                        return fieldModelController.$invalid && this.attempted;
                    } else {
                        return formController && formController.$invalid && this.attempted;
                    }
                };
                this.getErrMsg=function(ctrl){
                    var e=ctrl.$error;
                    var errMsg;
                    if (e.required){
                        errMsg='Please enter a value';
                    }
                    return errMsg;
                }
            }],
            compile: function(cElement, cAttributes, transclude) {
                return {
                    pre: function(scope, formElement, attributes, controllers) {
    
                        var submitController = controllers[0];
                        var formController = (controllers.length > 1) ? controllers[1] : null;
    
                        submitController.setFormController(formController);
    
                        scope.rc = scope.rc || {};
                        scope.rc[attributes.name] = submitController;
                    },
                    post: function(scope, formElement, attributes, controllers) {
    
                        var submitController = controllers[0];
                        var formController = (controllers.length > 1) ? controllers[1] : null;
                        var fn = $parse(attributes.rcSubmit);
    
                        formElement.bind('submit', function (event) {
                            submitController.setAttempted();
                            if (!scope.$$phase) scope.$apply();
    
                            if (!formController.$valid) return false;
    
                            scope.$apply(function() {
                                fn(scope, {$event:event});
                            });
                        });
                    }
              };
            }
        };
    }]
    };
    app.directive(rcSubmitDirective);
    
    0 讨论(0)
提交回复
热议问题