2

I've to use AngularJS 1.7 for a project where I have a form and have to validate an input field to be required, to have a particular pattern and to have unique values. For this I'm doing this :

  input#item-name(
    name="name",
    type="text",
    required,
    ng-pattern="$ctrl.pattern.id",
    ng-model="$ctrl.item.name",
  )

  div(ng-messages="addItemForm.name.$error")
    div(ng-messages-include="error-messages")

I've covered the required and the pattern one. But not able to include the unique validation.

I have an array with all the existing values :

this.existingItems = ['A','B','C];

I've tried something like this but did not work:

 input#item-name(
    name="name",
    type="text",
    required,
    ng-pattern="$ctrl.pattern.id",
    ng-model="$ctrl.item.name",
    ng-unique="$ctrl.existingItems",
  )

  div(ng-messages="addItemForm.name.$error")
    div(ng-messages-include="error-messages")

I've used this part of code inside a component and pointed "this" to "$ctrl".

3
  • Where is the documentation of ng-unique? What does it say? Commented Dec 7, 2018 at 12:21
  • I did not find any documentation for ng-unique. I tried it just like the pattern thinking it will work. Somewhere I've found that 'ensure-unique-values' are used but did not found any documentation for this either. Commented Dec 7, 2018 at 12:34
  • Yeah, that's what I thought. That's not a good way of programming. Instead, you should read the documentation. Find out that such a thing doesn't exist, but that it's possible to define your own validators, learn from the documentation how to do that, and try it out. You could achieve the same thing by not reading the documentation and try random things until you happen to do what the documentation says you should do, but it will be much much much longer. code.angularjs.org/1.7.1/docs/guide/forms#custom-validation Commented Dec 7, 2018 at 12:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.