Search code examples
javascripthtmlangularjsangularjs-directiverequired

AngularJS custom required text


I use the inbuilt AngularJs directive required, once it validates to false I get a small popup close to the field with the text "Please fill out this field". My problem is that I need the text in a different language, how should I proceed?

I have found some pretty good answers, but none that would solve this for the entire webapp. From my Angular experience I think that a custom directive which uses "setCustomValidity" should do the trick..


Solution

  • I figured out how to do it (note I use coffeescript):

    angular.module('myApp').directive('customRequiredText', () ->
      {
         restrict: 'AE'
         link: (scope, elem, attrs, ctrl) ->
           elem[0].oninvalid = (e) -> e.target.setCustomValidity('Fyll i det här fältet')
           elem[0].oninput = (e) -> e.target.setCustomValidity('')
     }
    )