• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!doctype html>
2<!--
3@license
4Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
5This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
6The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
7The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
8Code distributed by Google as part of the polymer project is also
9subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
10-->
11<html>
12<head>
13  <title>iron-form demo</title>
14
15  <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=yes">
16  <meta name="mobile-web-app-capable" content="yes">
17  <meta name="apple-mobile-web-app-capable" content="yes">
18
19  <script src="../../webcomponentsjs/webcomponents-lite.js"></script>
20  <link rel="import" href="../../iron-demo-helpers/demo-snippet.html">
21  <link rel="import" href="../../iron-demo-helpers/demo-pages-shared-styles.html">
22  <link rel="import" href="../../paper-input/paper-input.html">
23  <link rel="import" href="../../paper-button/paper-button.html">
24  <link rel="import" href="../../paper-checkbox/paper-checkbox.html">
25  <link rel="import" href="../../paper-dropdown-menu/paper-dropdown-menu.html">
26  <link rel="import" href="../../paper-item/paper-item.html">
27  <link rel="import" href="../../paper-listbox/paper-listbox.html">
28  <link rel="import" href="../../paper-spinner/paper-spinner.html">
29  <link rel="import" href="../../paper-styles/shadow.html">
30  <link rel="import" href="../iron-form.html">
31  <link rel="import" href="cats-only.html">
32
33  <custom-style>
34    <style is="custom-style" include="demo-pages-shared-styles">
35      input, paper-input, paper-checkbox {
36        margin-bottom: 8px;
37      }
38      iron-form {
39        @apply --shadow-elevation-2dp;
40        padding: 20px;
41      }
42      paper-button {
43        display: inline-block;
44        width: 45%;
45        text-align: center;
46      }
47      #form1 paper-button {
48        width: 30%;
49      }
50      paper-button:not([disabled]) {
51        background: #03a9f4;
52        color: white;
53      }
54      paper-spinner {
55        width: 14px;
56        height: 14px;
57        margin-right: 20px;
58      }
59      .output {
60        margin-top: 20px;
61        word-wrap: break-word;
62        font-family: monospace;
63      }
64    </style>
65  </custom-style>
66</head>
67<body unresolved>
68  <div class="vertical-section-container centered">
69    <h3>Iron-form works with native and custom elements, and can prevent automatic redirection</h3>
70    <demo-snippet>
71      <template>
72        <input type="checkbox" checked onchange="form1.allowRedirect = !this.checked;"> Prevent automatic form redirection<br><br>
73
74        <iron-form id="form1">
75          <form action="/foo" method="get">
76            <paper-input type="text" name="name" required label="Name" value="Batman"></paper-input>
77            <input name="foo" required>
78            <input type="checkbox" name="donuts" checked>I like donuts<br>
79            <paper-checkbox name="food" value="pizza" checked>pizza</paper-checkbox><br>
80            <paper-checkbox name="food" value="cheese" required>cheese</paper-checkbox>
81            <br>
82            <paper-dropdown-menu label="Cars" name="cars" required>
83              <paper-listbox class="dropdown-content" slot="dropdown-content">
84                <paper-item value="volvo">Volvo</paper-item>
85                <paper-item value="saab">Saab</paper-item>
86                <paper-item value="fiat">Fiat</paper-item>
87                <paper-item value="audi">Audi</paper-item>
88              </paper-listbox>
89            </paper-dropdown-menu>
90            <br><br>
91            <paper-button raised onclick="form1.submit()">Submit</paper-button>
92            <paper-button raised onclick="form1.reset()">Reset</paper-button>
93            <paper-button raised onclick="form1.validate()">Validate</paper-button>
94          </form>
95          <br>
96          <div class="output"></div>
97        </iron-form>
98        <script>
99          form1.addEventListener('iron-form-submit', function(event) {
100            this.querySelector('.output').innerHTML = JSON.stringify(event.detail);
101          });
102        </script>
103      </template>
104    </demo-snippet>
105
106    <h3>You can submit a form in many different ways: by manually calling submit(),
107      using a native button, or by wrapping a paper-button in a native button:</h3>
108    <demo-snippet>
109      <template>
110        <iron-form id="form2">
111          <form action="/foo" method="get">
112            <paper-input label="Name" value="Batman" name="name"></paper-input>
113            <input type="checkbox" required> You must check this box.
114            <p>Using a native button to submit the form will display
115            the native browser validation UI for native elements: </p>
116            <button type="submit">Submit</button>
117            <button type="reset">Reset</button>
118
119            <p>Using a custom element to submit the form will not display
120            the native browser validation UI:</p>
121            <paper-button raised onclick="form2.submit()">Submit</paper-button>
122            <paper-button raised onclick="form2.reset()">Reset</paper-button>
123          </form>
124          <br>
125          <div class="output"></div>
126        </iron-form>
127        <script>
128          form2.addEventListener('iron-form-submit', function(event) {
129            this.querySelector('.output').innerHTML = JSON.stringify(event.detail);
130          });
131        </script>
132      </template>
133    </demo-snippet>
134
135    <h3>To customize the request sent to the server, you can listen to the `iron-form-presubmit` event</h3>
136    <demo-snippet>
137      <template>
138        <iron-form id="form3">
139          <form action="/foo" method="get">
140            <paper-input name="name" label="Name" value="Batman" required></paper-input>
141            <paper-button raised onclick="form3.submit()">Submit</paper-button>
142            <paper-button raised onclick="form3.reset()">Reset</paper-button>
143          </form>
144          <div class="output"></div>
145        </iron-form>
146        <script>
147          form3.addEventListener('iron-form-presubmit', function(event) {
148            this.request.params['sidekick'] = 'Robin';
149          });
150          form3.addEventListener('iron-form-submit', function(event) {
151            this.querySelector('.output').innerHTML = JSON.stringify(event.detail);
152          });
153        </script>
154      </template>
155    </demo-snippet>
156
157    <h3>Example of an <code>iron-form</code> reacting to state changes.</h3>
158    <p>This form will have the "Submit" button disabled until all fields
159      are valid, and then show a spinner during submission.</p>
160
161    <demo-snippet>
162      <template>
163        <iron-form id="form4">
164          <form action="/foo" method="get">
165            <paper-input name="name" label="Name" required auto-validate></paper-input>
166            <paper-checkbox name="read" required>You must check this box</paper-checkbox><br>
167
168            <paper-button raised onclick="_delayedSubmit(event)" disabled id="form4Submit">
169              <paper-spinner id="spinner" hidden></paper-spinner>Submit</paper-button>
170            <paper-button raised onclick="form4.reset()">Reset</paper-button>
171          </form>
172          <div class="output"></div>
173        </iron-form>
174        <script>
175          form4.addEventListener('iron-form-submit', function(event) {
176            this.querySelector('.output').innerHTML = JSON.stringify(event.detail);
177            spinner.active = false;
178            spinner.hidden = true;
179            form4Submit.disabled = false;
180          });
181          form4.addEventListener('change', function(event) {
182            // Validate the entire form to see if we should enable the `Submit` button.
183            form4Submit.disabled = !form4.validate();
184          });
185          function _delayedSubmit(event) {
186            spinner.active = true;
187            spinner.hidden = false;
188            form4Submit.disabled = true;
189            // Simulate a slow server response.
190            setTimeout(function() {
191              form4.submit()
192            }, 1000);
193          }
194        </script>
195      </template>
196    </demo-snippet>
197
198
199    <h3>Iron-form respects the novalidate form attribute</h3>
200    <demo-snippet>
201      <template>
202        <iron-form id="form5">
203          <form method="get" action="/foo" novalidate>
204            <paper-input name="name" label="Name" required></paper-input>
205            <cats-only name="cats"></cats-only>
206            <input name="animal" placeholder="animal" required value="meerkat"><br>
207            <paper-checkbox name="cheese" required>Cheese</paper-checkbox>
208            <br>
209            <paper-button raised onclick="form5.submit()">Submit</paper-button>
210            <paper-button raised onclick="form5.reset()">Reset</paper-button>
211          </form>
212          <div class="output"></div>
213        </iron-form>
214        <script>
215          form5.addEventListener('iron-form-submit', function(event) {
216            this.querySelector('.output').innerHTML = JSON.stringify(event.detail);
217          });
218        </script>
219      </template>
220    </demo-snippet>
221
222    <h3>Iron-form respects nested elements</h3>
223    <demo-snippet>
224      <template>
225        <iron-form id="form6">
226          <form method="get" action="/foo">
227            <p>
228              <input name="name" label="Name" required>
229              <cats-only name="cats"></cats-only>
230              <paper-checkbox name="cheese" required>Cheese</paper-checkbox>
231            </p>
232
233            <input type="submit" value="Submit">
234            <br>
235            <paper-button raised onclick="form6.submit()">Submit</paper-button>
236            <paper-button raised onclick="form6.reset()">Reset</paper-button>
237          </form>
238          <div class="output"></div>
239        </iron-form>
240        <script>
241          form6.addEventListener('iron-form-submit', function(event) {
242            this.querySelector('.output').innerHTML = JSON.stringify(event.detail);
243          });
244        </script>
245      </template>
246    </demo-snippet>
247  </div>
248
249</body>
250</html>
251