Skip to content
This repository has been archived by the owner on Nov 24, 2018. It is now read-only.

valdrMessage should display builtin angular validation errors like 'number' #58

Closed
bjorndown opened this issue Jan 14, 2015 · 3 comments

Comments

@bjorndown
Copy link

I have a form field

<form name="foo" novalidate valdr-type="Foo">
    <div valdr-form-group>
        <input class="form-control" type="number" name="bar"/>
    </div>
</form>

with validation rules

  "Foo": {
    "bar": {
      "min": {
        "value": 1
      }
    }
  }

and I use valdrMessage for displaying errors.

When I enter "a" into the field, then valdrMessage doesn't display anything even though the form is invalid. This is because type="number" trigger angular's number validator and valdr's min-validator is not even called. This is confusing if you want to use only valdrMessage for displaying validation errors.

Therefore it would be handy if valdrMessage knew about angular's builtin validations.

Problem:
valdrMessage relies on message keys from the constraints-JSON for displaying errror messages. Since there are none for angular's builtin validation, what should it display?

What do you recommend?

@philippd
Copy link
Collaborator

It should be pretty straightforward to also show other validation errors than those from valdr validators in the message template. We can read all violations from the ngModelController in the valdrMessage directive and provide them to the message templates.

What's missing now are the error messages. I'd suggest that we extend the valdrMessage service with a new function where the user can register error messages by validator name.

valdrMessage.addMessage('required', 'This field is required.');

The validator name could be number, minlength, required or any other name of built-in or custom AngularJS validators.

Message inheritance could be done similar to how ngMessages does it: The default message is the one registered for the validator name. If you need a more specific message on field level, you can register it using valdrs type and field name:

valdrMessage.addMessage('Person.firstName.required', 'The first name is required.');

@bjorndown
Copy link
Author

Cool, I like it.

Just to be sure: valdrMessage.addMessage will work with translation keys as well, right? Looking at the code I'd say yes.

@philippd
Copy link
Collaborator

Just to be sure: valdrMessage.addMessage will work with translation keys as well, right? Looking at the code I'd say yes

Yes.

@philippd philippd modified the milestone: 1.1.2 Jan 16, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants