Skip to content

Instantly share code, notes, and snippets.

@oivoodoo
Created February 17, 2014 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oivoodoo/9049917 to your computer and use it in GitHub Desktop.
Save oivoodoo/9049917 to your computer and use it in GitHub Desktop.
required.coffee
admin.directive 'gpnRequired', ['$compile', ($compile) ->
isEmpty = (value) ->
angular.isUndefined(value) || value is '' || value is null || (angular.isArray(value) && value.length is 0)
link = ($scope, $element, $attributes, $control) ->
title = I18n.t('js.required')
name = $scope.$eval($attributes.gpnRequried) || $attributes.gpnRequired
template = "<span class = 'error' ng-show = \"form['#{name}'].$error.gpnRequired\">#{title}</span>"
angular.element($compile(template)($scope)).insertAfter($element)
validator = (isRequired, value) ->
inValid = isRequired && isEmpty(value)
$control.$setValidity('gpnRequired', !inValid)
validate = ->
isRequired = $scope.$eval($attributes.if || 'true')
validator(isRequired, $control.$viewValue)
$scope.$watch($attributes.ngModel, validate)
if $attributes.if?
$scope.$watch($attributes.if, validate)
return {
restrict: 'A'
require: 'ngModel'
link: link
replace: false
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment