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

Remove Validator from Element

Ghislain B. edited this page May 18, 2015 · 16 revisions

Removing a Validator can work for both the Directive and the Service but in all situation you need to call it through the controller and for that matter you still need to use the validationService and the call to 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');

    // or use the removeInputValidator() function as shown over
});

####Service Almost identical, except that you probably already have a reference to your validationService object.

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');

    // or use the removeInputValidator() function as shown over
});
Clone this wiki locally