Rolling forward https://codereview.chromium.org/1155683008/ which was
[chromium-blink-merge.git] / third_party / polymer / v1_0 / components / iron-validatable-behavior / iron-validatable-behavior.html
blobd792b0daa5447b20ee1f9cb42ab6a3a1d2ef8061
1 <!--
2 @license
3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
4 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7 Code distributed by Google as part of the polymer project is also
8 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9 -->
11 <link rel="import" href="../polymer/polymer.html">
12 <link rel="import" href="../iron-meta/iron-meta.html">
14 <script>
16 /**
17 * Use `Polymer.IronValidatableBehavior` to implement an element that validates user input.
19 * ### Accessiblity
21 * Changing the `invalid` property, either manually or by calling `validate()` will update the
22 * `aria-invalid` attribute.
24 * @demo demo/index.html
25 * @polymerBehavior
27 Polymer.IronValidatableBehavior = {
29 properties: {
31 /**
32 * Namespace for this validator.
34 validatorType: {
35 type: String,
36 value: 'validator'
39 /**
40 * Name of the validator to use.
42 validator: {
43 type: String
46 /**
47 * True if the last call to `validate` is invalid.
49 invalid: {
50 reflectToAttribute: true,
51 type: Boolean,
52 value: false
55 _validatorMeta: {
56 type: Object
61 observers: [
62 '_invalidChanged(invalid)'
65 get _validator() {
66 return this._validatorMeta && this._validatorMeta.byKey(this.validator);
69 ready: function() {
70 this._validatorMeta = new Polymer.IronMeta({type: this.validatorType});
73 _invalidChanged: function() {
74 if (this.invalid) {
75 this.setAttribute('aria-invalid', 'true');
76 } else {
77 this.removeAttribute('aria-invalid');
81 /**
82 * @return {Boolean} True if the validator `validator` exists.
84 hasValidator: function() {
85 return this._validator != null;
88 /**
89 * @param {Object} values Passed to the validator's `validate()` function.
90 * @return {Boolean} True if `values` is valid.
92 validate: function(values) {
93 var valid = this._validator && this._validator.validate(values);
94 this.invalid = !valid;
95 return valid;
100 </script>