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

Alternate Text on Validators

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

Validators can now use alternate text instead of the usual generic locale error messages, for example seeing "Field is Required" on a <select> might not always be useful, though it might be more useful to see an alternate text that is "Please choose an option". Alternate Text works on all type of validators and are defined by adding :alt= at the end of any Validator (including Custom Regular Expression Pattern as well), it could be used on 1 or more Validator(s) directly inside the input validation="" attribute. See the examples below.

Directive
<!-- You can use translate in your HTML -->
<!-- Example #1 with 1 alternate text on 1 of the 2 validators -->
<input name="input1" validation="alpha|required:alt=Your Alternate Required Text." />

<!-- Example #2, alternate text on multiple validators -->
<input name="input1" validation="date_iso_between:2015-03-01,2015-03-30:alt=Booking date must be in April|required:alt=Booking Date is Required" />

<!-- Example #3, use $translate as alternate text -->
<input name="input1" validation="min_len:5|required:alt={{ 'YOUR_TEXT' | translate }}" />
Service
// inject the ValidationService inside your Controller
myApp.controller('CtrlValidationService', function ($scope, $translate, ValidationService) {
  // Example #1 with 1 alternate text on 1 of the 2 validators
  myValidationService.addValidator('input1', 'alpha|required:alt=Your Alternate Required Text.');

  // Example #2, alternate text on multiple validators
  myValidationService.addValidator('input1', 'date_iso_between:2015-03-01,2015-03-30:alt=Booking date must be in April|required:alt=Booking Date is Required');

  // Example #3, use $translate as alternate text
  // you can use the $translate.instant() function
  myValidationService.addValidator('input1', 'min_len:5|required:alt=' + $translate.instant('YOUR_TEXT'));
});
Clone this wiki locally