Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

ngMaxlength attribute as separate reusable directive. #6750

Closed
@pluff

Description

@pluff

Currently ngMaxlength validation can be applied to inputs only, so can't be applied to custom controls. So if I want to use it with ngModel without input field I have to write custom 100% duplicated separate directive and use it. It would be great to have this validator to be applicable to anything that has ngModel attached, not only inputs.

Same applies to ngMinlength etc.

Example directive I have to write:

app.directive 'myMaxlength', ->
  {
    restrict: 'A',
    require: 'ngModel',
    link: (scope, el, attrs, ctrl) ->
      maxlength = parseInt(attrs.myMaxlength);
      validator = (value) ->
        if ctrl.$isEmpty(value) || value.length <= maxlength
          ctrl.$setValidity 'maxlength', true
          value
        else
          ctrl.$setValidity 'maxlength', false
          undefined
      ctrl.$formatters.push validator
      ctrl.$parsers.push validator
  }

which almost fully duplicates https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L1009-L1018

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions