$setValidity in directive and 'validationErrorKey'

1,985 views
Skip to first unread message

Adriaan Bouman

unread,
Aug 1, 2012, 4:51:24 AM8/1/12
to ang...@googlegroups.com
Hi all,

I am using the following in my custum input directive: controller.$setValidity(false); As result the input and form gets invalidated, as I was hoping for!

In the documentation and API it says I need to supply a 'validationErrorKey'. Is this really needed or is the above also a valid practise?

Thanks for clarifying!

greetings,
Adriaan

Pawel Kozlowski

unread,
Aug 31, 2012, 1:50:24 PM8/31/12
to ang...@googlegroups.com
hi!



On Wed, Aug 1, 2012 at 10:51 AM, Adriaan Bouman
<adriaan...@gmail.com> wrote:
> Hi all,
>
> I am using the following in my custum input directive:
> controller.$setValidity(false); As result the input and form gets
> invalidated, as I was hoping for!

It works but kind of "by chance", check the code here:
https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L937

> In the documentation and API it says I need to supply a
> 'validationErrorKey'. Is this really needed or is the above also a valid
> practise?

'validationErrorKey' is really needed as soon as you want to display
precise error messages to your users. The 'validationErrorKey' makes
it possible to distinguish different validation errors for the same
field (for example: id a field is required and of type email you might
have 2 error conditions: the field not filled in at all or provided
value not being a valid email). So yes, I would say it is pretty much
mandatory to provide 'validationErrorKey' if you want your directive
to be really user-friendly. Check the syntax for validation error
here: http://docs.angularjs.org/api/ng.directive:input

Hope this helps,
Pawel

--
Question? Send a fiddle
(http://jsfiddle.net/pkozlowski_opensource/Q2NpJ/) or a plunk
(http://plnkr.co/)
Need help with jsFiddle? Check this:
http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/

Looking for UI widget library for AngularJS? Here you go:
http://angular-ui.github.com/
Reply all
Reply to author
Forward
0 new messages