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

Global Options

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

To change default options globally, you can use the $scope.$validationOptions. These global options can be defined for both the Directive and/or the Service.

For more details about the ControllerAs option, please refer to the Wiki - ControllerAs Syntax

Available Global Options

// use the ControllerAs syntax
controllerAs: vm,

// set the debounce globally
debounce: 1500,

// separator used between each error messages (default to space ' ')
errorMessageSeparator: ' ',
    
// display only last error message on each input element (False default)
// for example if 3 errors are on the element, only the last error would show up
displayOnlyLastErrorMsg: false,

// providing a formName helps Angular-Validation with the validationSummary
// mandatory when validating an external 3rd party addon
formName: 'yourFormName',

// hide error messages showing under input elements (False by default)
// useful when we want to use Validation Summary only
hideErrorUnderInputs: false,

// set which scope Angular-Validation will use
// mainly used by $validationSummary and by checkFormValidity(), resetForm(), removeValidator()
isolatedScope: $scope,         

// pre-validate all form elements (False by default)
preValidateFormElements: false,

// pre-validate Validation Summary, if set to True then all errors show up instantly (True by default)
preValidateValidationSummary: true,

// reset Global Options on Route Change (True by default)
resetGlobalOptionsOnRouteChange: true,

// do we want to validate the field even when field is empty? False by default
// could be useful on `custom` or `remote` Validators.
validateOnEmpty: false

Examples

Directive/Service
myApp.controller('Ctrl', function ($scope) {
  $scope.$validationOptions = { debounce: 1500, /* etc... */ }; 
});

All of these options can also be defined using the Service.

Service
// inject the ValidationService inside your Controller
myApp.controller('Ctrl', function ($scope, ValidationService) {
  // start by creating the service
  var myValidation = new ValidationService();

  // define the scope and isolatedScope, the scope property always needs to be there
  myValidation.setGlobalOptions({ debounce: 1500, /* etc... */ });

}); 

Instead of calling the setGlobalOptions(), you could alternatively also directly use the ValidationService Constructor which is much shorter syntax:

// using the constructor
var myValidation = new ValidationService({ controllerAs: vm, debounce: 1500 });
Clone this wiki locally