Skip nested forms validation with AngularJS

后端 未结 12 2088
失恋的感觉
失恋的感觉 2020-12-23 18:16

How can I skip validation of nested forms with AngularJS? I have to make an outer form valid even when its child form is invalid.

In the example below outer form sh

12条回答
  •  情书的邮戳
    2020-12-23 18:38

    I found out the solution that worked best was Anton's.

    Setting the nullFormCtrl suggested by mbernath disables validation on the child form (thxs for paving the way though...).

    The only change that I made was in the way the parentForm is accessed. angular does provide a method for that.

    .directive('isolateForm', [function () {
        return {
            restrict: 'A',
            require: '?form',
            link: function link(scope, element, iAttrs, formController) {
    
                if (!formController) {
                    return;
                }
    
                // Remove this form from parent controller
                formController.$$parentForm.$removeControl(formController)
    
                var _handler = formController.$setValidity;
                formController.$setValidity = function (validationErrorKey, isValid, cntrl) {
                    _handler(validationErrorKey, isValid, cntrl);
                    formController.$$parentForm.$setValidity(validationErrorKey, true, this);
                }
            }
        };
    }]);

提交回复
热议问题