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

PreValidate Form (on page load)

Ghislain B. edited this page Jul 21, 2015 · 16 revisions

Stable since version 1.3.27+

By default Angular-Validation does not display any error at first, at least not until user start typing test in each of the Form Elements. You can change this behavior and display the error right from the beginning by simply change a global option.

######Directive Change the Angular-Validation default options

myApp.controller('Ctrl', function ($scope) {
  $scope.$validationOptions = { 
    debounce: 1500,                // set the debounce globally
    preValidateFormElements: true  // pre-validate all form elements, false by default
  }; 
});

######Service From the Service you can also change it by the following ways. P.S. This only works when all your input elements have been defined by the Angular-Validation Service

myApp.controller('Ctrl', function ($scope, validationService) {
  // start by creating the service
  var myValidation = new validationService();

  // set property of preValidateFormElements (false by default)
  myValidation.setGlobalOptions({ debounce: 1500, scope: $scope, preValidateFormElements: true });
});

####Note - How to reset a Form? Angular-Validation is using the $touched property for showing/hiding the errors, so if you want to reset your form then you should use the $setUntouched() on your Form and you probably want to use the $setPristine() as well. As shown in the Angular Guide, you can do the following

HTML

<input type="button" ng-click="reset(form)" value="Reset" />

Javascript (inside Controller)

$scope.reset = function(form) {
    if (form) {
      form.$setPristine();
      form.$setUntouched();
    }
};
Clone this wiki locally