|
| 1 | +<!doctype html> |
| 2 | +<!-- |
| 3 | +@license |
| 4 | +Copyright (c) 2015 The Polymer Project Authors. All rights reserved. |
| 5 | +This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt |
| 6 | +The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 7 | +The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt |
| 8 | +Code distributed by Google as part of the polymer project is also |
| 9 | +subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt |
| 10 | +--> |
| 11 | +<html> |
| 12 | +<head> |
| 13 | + |
| 14 | + <meta charset="utf-8"> |
| 15 | + <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> |
| 16 | + <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=yes"> |
| 17 | + |
| 18 | + <title>iron-validator-behavior demo</title> |
| 19 | + |
| 20 | + <script src="../../webcomponentsjs/webcomponents-lite.js"></script> |
| 21 | + |
| 22 | + <link rel="import" href="../../iron-meta/iron-meta.html"> |
| 23 | + <link rel="import" href="cats-only.html"> |
| 24 | + |
| 25 | + <link rel="stylesheet" href="../../paper-styles/demo.css"> |
| 26 | + |
| 27 | + <style> |
| 28 | + |
| 29 | + .valid { |
| 30 | + color: limegreen; |
| 31 | + } |
| 32 | + |
| 33 | + .invalid { |
| 34 | + color: red; |
| 35 | + } |
| 36 | + |
| 37 | + </style> |
| 38 | + |
| 39 | +</head> |
| 40 | +<body> |
| 41 | + |
| 42 | + <template is="x-autobind"> |
| 43 | + |
| 44 | + <cats-only></cats-only> |
| 45 | + |
| 46 | + <section> |
| 47 | + |
| 48 | + <p> |
| 49 | + only type <code>cats</code>: |
| 50 | + |
| 51 | + <input on-input="_onInput"> |
| 52 | + |
| 53 | + <span class="valid" hidden$="[[!valid]]">valid</span> |
| 54 | + <span class="invalid" hidden$="[[valid]]">invalid</span> |
| 55 | + </p> |
| 56 | + |
| 57 | + </section> |
| 58 | + |
| 59 | + <section> |
| 60 | + |
| 61 | + <p> |
| 62 | + only type <code>cats</code> across both input fields: |
| 63 | + |
| 64 | + <span on-input="_onInputMulti"> |
| 65 | + <input> |
| 66 | + <input> |
| 67 | + </span> |
| 68 | + |
| 69 | + <span class="valid" hidden$="[[!validMulti]]">valid</span> |
| 70 | + <span class="invalid" hidden$="[[validMulti]]">invalid</span> |
| 71 | + </p> |
| 72 | + |
| 73 | + </section> |
| 74 | + |
| 75 | + <section> |
| 76 | + |
| 77 | + <p> |
| 78 | + only type <code>cats</code> in the form: |
| 79 | + |
| 80 | + <form on-submit="_onSubmit"> |
| 81 | + <label> |
| 82 | + Type something: <input name="something"> |
| 83 | + </label> |
| 84 | + <br> |
| 85 | + <label> |
| 86 | + Your favorite pet: |
| 87 | + <select name="pet"> |
| 88 | + <option>iguanas</option> |
| 89 | + <option>cats</option> |
| 90 | + <option>pancakes</option> |
| 91 | + </select> |
| 92 | + </label> |
| 93 | + <br> |
| 94 | + <button type="submit">submit!</button> |
| 95 | + <span class="valid" hidden$="[[!validForm]]">valid</span> |
| 96 | + <span class="invalid" hidden$="[[validForm]]">invalid</span> |
| 97 | + </form> |
| 98 | + |
| 99 | + </p> |
| 100 | + |
| 101 | + </section> |
| 102 | + |
| 103 | + </template> |
| 104 | + |
| 105 | + <script> |
| 106 | + |
| 107 | + var validator = new Polymer.IronMeta({type: 'validator'}).byKey('cats-only'); |
| 108 | + |
| 109 | + var scope = document.querySelector('template[is=x-autobind]'); |
| 110 | + scope.valid = scope.validMulti = scope.validForm = true; |
| 111 | + |
| 112 | + scope._onInput = function(event) { |
| 113 | + this.valid = validator.validate(event.target.value); |
| 114 | + }; |
| 115 | + |
| 116 | + scope._onInputMulti = function(event) { |
| 117 | + var values = []; |
| 118 | + var nodes = Polymer.dom(event.currentTarget).querySelectorAll('input'); |
| 119 | + for (var node, i = 0; node = nodes[i]; i++) { |
| 120 | + values.push(node.value); |
| 121 | + } |
| 122 | + this.validMulti = validator.validate(values); |
| 123 | + }; |
| 124 | + |
| 125 | + scope._onSubmit = function(event) { |
| 126 | + event.preventDefault(); |
| 127 | + |
| 128 | + var data = {}; |
| 129 | + for (var el, i = 0; el = event.target.elements[i]; i++) { |
| 130 | + if (el.name) { |
| 131 | + data[el.name] = el.value; |
| 132 | + } |
| 133 | + } |
| 134 | + this.validForm = validator.validate(data); |
| 135 | + }; |
| 136 | + |
| 137 | + </script> |
| 138 | + |
| 139 | +</body> |
0 commit comments