Skip to content
This repository has been archived by the owner on Jul 1, 2020. It is now read-only.

Remove Validator from Element

Ghislain B edited this page Mar 23, 2017 · 16 revisions

Support both Directive & Service since version 1.3.29+

Removing a Validator can work for both the Directive and the Service but in all situation you need make a call through your Controller and then use the ValidationService to call the removeValidator() function by passing your Form object and input name.

For example, we can use a simple button to remove a validator, like so:

<!-- Add a remove button in your html -->
<span class="text-right">
    <button ng-click="removeInputValidator('input2');">
        Remove Input2 Validator
    </button>
</span>

possibly make a remove function inside your controller

// you can also remove a Validator with an ngClick or whichever way you prefer by calling .removeValidator()
  $scope.removeInputValidator = function ( elmName ) {
    // 1st argument is the object holding our $validationSummary `$scope.yourFormName`
    //   If your form does not have a name attribute, your only choice is to use `$scope` as argument
    // 2nd argument, remove a single element (string)
    //    OR you can also remove multiple elements through an array type .removeValidator($scope.form1, ['input2','input3'])
    myValidation.removeValidator($scope.form1, elmName);
  };

Directive

<!-- a simple input that we want to remove a validator -->
<form name="form1"
   <input type="text" name="input1" ng-model="input1" validation="min_len:2|max_len:10|alpha_dash_spaces|required" />
   <input type="text" name="input2" ng-model="input2" validation="required" />
</form>
myApp.controller('CtrlDirective', function($scope, ValidationService) {
    // remove a defined validator by the form and element name
    new ValidationService().removeValidator($scope.form1, 'input1');
});

Service

Almost identical, except that you probably already have a reference to your ValidationService object.

// inject the ValidationService inside your Controller
myApp.controller('Ctrlservice', function($scope, ValidationService) {
    // you need reference to your previous Service object variable
    var myValidation = new ValidationService();
    myValidation
        .setGlobalOptions({ debounce: 1500, scope: $scope })
        .addValidator('input1', 'min_len:2|max_len:10|alpha_dash_spaces|required')
        .addValidator('input2', 'required')

    // remove a defined validator by the form and element name
    myValidation.removeValidator($scope.form1, 'input1');
});
Clone this wiki locally