forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforms.amp.html
539 lines (494 loc) · 19.4 KB
/
forms.amp.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
<!doctype html>
<html ⚡>
<head>
<meta charset="utf-8">
<title>Forms Examples in AMP</title>
<link rel="canonical" href="amps.html" >
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-anim" src="https://cdn.ampproject.org/v0/amp-anim-0.1.js"></script>
<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
<script async custom-element="amp-lightbox" src="https://cdn.ampproject.org/v0/amp-lightbox-0.1.js"></script>
<script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.1.js"></script>
<style amp-custom>
input.user-invalid {
background-color: #dc4e41;
}
input.user-valid {
background-color: #4CAF50;
}
fieldset.user-invalid {
border: 3px solid #dc4e41;
}
fieldset.user-valid {
border: 3px solid #4CAF50;
}
form .form-message {
display: none;
}
form.user-invalid .invalid-message {
display: block;
color: #dc4e41;
}
form.user-valid .valid-message {
display: block;
color: #4CAF50;
}
form.amp-form-submitting .submitting-message {
display: block;
color: #919191;
}
form.amp-form-submit-success [submit-success] {
color: #0000ff;
}
form.amp-form-submit-error [submit-error] {
color: #dc4e41;
}
.lightbox {
background: rgba(0,0,0,.8);
}
.lightbox-content {
display: flex;
flex-direction: column;
flex-wrap: nowrap;
justify-content: center;
align-items: center;
color: white;
}
#poll1 li {
position: relative;
padding: 1em;
list-style: none;
margin: 0;
display: block;
height: 20px;
}
#poll1.amp-form-submit-success fieldset {
display: none;
}
.percentage-container {
z-index: 0;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.one-pc {
width: 1%;
height: 100%;
display: inline-block;
}
.Wekas .one-pc {
background: #dc4e41;
}
.Penguins .one-pc {
background: #3f51b5;
}
.Ostriches .one-pc {
background: #4CAF50;
}
.Kiwis .one-pc {
background: #00ffff;
}
.poll1-results .title {
z-index: 1;
color: black;
position: absolute;
}
</style>
</head>
<body>
<amp-analytics>
<script type="application/json">
{
"requests": {
"event": "https://www.ampproject.org/static/img/logo-blue.svg?eid=${eventId}"
},
"triggers": {
"formSubmitSuccess": {
"on": "amp-form-submit-success",
"request": "event",
"vars": {
"eventId": "form-submit-success"
}
},
"formSubmitError": {
"on": "amp-form-submit-error",
"request": "event",
"vars": {
"eventId": "form-submit-error"
}
}
}
}
</script>
</amp-analytics>
<h4>Search (GET non-xhr same origin)</h4>
<form method="get" action="/form/search-html/get" target="_blank">
<fieldset>
<label>
<span>Search for</span>
<input type="search" name="term" required>
</label>
<input type="submit" value="Search">
</fieldset>
</form>
<h4>Search (GET non-xhr cross origin - current host > httpbin.org)</h4>
<form method="get" action="https://httpbin.org/html" target="_blank">
<fieldset>
<label>
<span>Search for</span>
<input type="search" name="term" required>
</label>
<input type="submit" value="Search">
</fieldset>
</form>
<h4>Search (GET xhr same origin)</h4>
<form method="get" action="/form/search-html/get" action-xhr="/form/search-json/get" target="_blank">
<fieldset>
<label>
<span>Search for</span>
<input type="search" name="term" required>
</label>
<input type="submit" value="Search">
</fieldset>
<div submit-success>
<template type="amp-mustache">
<h1>Here are the results for the search:</h1>
<ul>
{{#results}}<li>{{title}}</li>{{/results}}
</ul>
</template>
</div>
</form>
<h4>Search (GET xhr cross origin current host > httpbin.org)</h4>
<form method="get"
action="https://httpbin.org/response-headers?AMP-Access-Control-Allow-Source-Origin=http%3A%2F%2Flocalhost%3A8000&access-control-expose-headers=AMP-Access-Control-Allow-Source-Origin"
action-xhr="https://httpbin.org/response-headers?AMP-Access-Control-Allow-Source-Origin=http%3A%2F%2Flocalhost%3A8000&access-control-expose-headers=AMP-Access-Control-Allow-Source-Origin"
target="_blank">
<fieldset>
<label>
<span>Search for</span>
<input type="search" name="term" required>
</label>
<input type="submit" value="Search">
</fieldset>
<div submit-success>
<template type="amp-mustache">
<h1>You searched for: {{term}}</h1>
</template>
</div>
</form>
<h4>Subscribe to our weekly Newsletter (POST xhr same origin)</h4>
<form method="post"
action-xhr="/form/echo-json/post"
target="_blank">
<fieldset>
<label>
<span>Your name</span>
<input type="text" name="name" id="name1" required>
</label>
<label>
<span>Your email</span>
<input type="email" name="email" id="email1" required>
</label>
<input type="submit" value="Subscribe">
</fieldset>
<div submit-success>
<template type="amp-mustache">
Success! Thanks {{name}} for subscribing! Please make sure to check your email {{email}}
to confirm!
</template>
</div>
<div submit-error>
<template type="amp-mustache">
Oops! {{name}}, We apologies something went wrong. Please try again later.
</template>
</div>
</form>
<h4>Subscribe to our weekly Newsletter (xhr POST cross origin - current host > amp.localhost:8000)</h4>
<form method="post"
action-xhr="http://amp.localhost:8000/form/echo-json/post"
target="_blank">
<fieldset>
<label>
<span>Your name</span>
<input type="text" name="name" id="name2" required>
</label>
<label>
<span>Your email</span>
<input type="email" name="email" id="email2" required>
</label>
<input type="submit" value="Subscribe">
</fieldset>
<div submit-success>
<template type="amp-mustache">
Success! Thanks {{name}} for subscribing! Please make sure to check your email {{email}}
to confirm!
</template>
</div>
<div submit-error>
<template type="amp-mustache">
Oops! {{name}}, We apologies something went wrong. Please try again later.
</template>
</div>
</form>
<h4>Subscribe to our weekly Newsletter (xhr + submit events)</h4>
<form method="post"
action-xhr="/form/echo-json/post"
target="_blank"
on="submit-success:success-lightbox;submit-error:error-lightbox">
<fieldset>
<label>
<span>Your name</span>
<input type="text" name="name" id="name3" required>
</label>
<label>
<span>Your email</span>
<input type="email" name="email" id="email3" required>
</label>
<input type="submit" value="Subscribe">
</fieldset>
<div class="form-message invalid-message">
There are some input that needs fixing. Please fix them.
</div>
<div class="form-message valid-message">
Everything looks good, you can submit now!
</div>
<div class="form-message submitting-message">
Please hold on while we submit your answer!
</div>
<div submit-success>
<template type="amp-mustache">
Success! Thanks {{name}} for subscribing! Please make sure to check your email {{email}}
to confirm!
</template>
</div>
<div submit-error>
<template type="amp-mustache">
Oops! {{name}}, We apologies something went wrong. Please try again later.
</template>
</div>
</form>
<amp-lightbox id="success-lightbox" class="lightbox" layout="nodisplay">
<div class="lightbox-content">
<h1>Thanks for the submission! Here's a cupcake!</h1>
<amp-img id="img0" src="https://lh3.googleusercontent.com/5rcQ32ml8E5ONp9f9-Rf78IofLb9QjS5_0mqsY1zEFc=w400-h600-no-n" width=400 height=600 layout="fixed" on="tap:success-lightbox.close" role="img" tabindex="1"></amp-img>
</div>
</amp-lightbox>
<amp-lightbox id="error-lightbox" class="lightbox" layout="nodisplay">
<div class="lightbox-content">
<h1>Oops! Kittens!</h1>
<amp-anim
src="https://lh3.googleusercontent.com/qNn8GDz8Jfd-s9lt3Nc4lJeLjVyEaqGJTk1vuCUWazCmAeOBVjSWDD0SMTU7x0zhVe5UzOTKR0n-kN4SXx7yElvpKYvCMaRyS_g-jydhJ_cEVYmYPiZ_j1Y9de43mlKxU6s06uK1NAlpbSkL_046amEKOdgIACICkuWfOBwlw2hUDfjPOWskeyMrcTu8XOEerCLuVqXugG31QC345hz3lUyOlkdT9fMYVUynSERGNzHba7bXMOxKRe3izS5DIWUgJs3oeKYqA-V8iqgCvneD1jj0Ff68V_ajm4BDchQubBJU0ytXVkoWh27ngeEHubpnApOS6fcGsjPxeuMjnzAUtoTsiXz2FZi1mMrxrblJ-kZoAq1DJ95cnoqoa2CYq3BTgq2E8BRe2paNxLJ5GXBCTpNdXMpVJc6eD7ceijQyn-2qanilX-iK3ChbOq0uBHMvsdoC_LsFOu5KzbbNH71vM3DPkvDGmHJmF67Vj8sQ6uBrLnzpYlCyN4-Y9frR8zugDcqX5Q=w400-h300-no"
width="400" height="300" on="tap:error-lightbox.close"
tabindex="2" role="img">
<amp-img placeholder
src="https://lh3.googleusercontent.com/qNn8GDz8Jfd-s9lt3Nc4lJeLjVyEaqGJTk1vuCUWazCmAeOBVjSWDD0SMTU7x0zhVe5UzOTKR0n-kN4SXx7yElvpKYvCMaRyS_g-jydhJ_cEVYmYPiZ_j1Y9de43mlKxU6s06uK1NAlpbSkL_046amEKOdgIACICkuWfOBwlw2hUDfjPOWskeyMrcTu8XOEerCLuVqXugG31QC345hz3lUyOlkdT9fMYVUynSERGNzHba7bXMOxKRe3izS5DIWUgJs3oeKYqA-V8iqgCvneD1jj0Ff68V_ajm4BDchQubBJU0ytXVkoWh27ngeEHubpnApOS6fcGsjPxeuMjnzAUtoTsiXz2FZi1mMrxrblJ-kZoAq1DJ95cnoqoa2CYq3BTgq2E8BRe2paNxLJ5GXBCTpNdXMpVJc6eD7ceijQyn-2qanilX-iK3ChbOq0uBHMvsdoC_LsFOu5KzbbNH71vM3DPkvDGmHJmF67Vj8sQ6uBrLnzpYlCyN4-Y9frR8zugDcqX5Q=w400-h300-no-k"
width="400" height="300"></amp-img>
</amp-anim>
</div>
</amp-lightbox>
<h4>NOT ALLOWED (POST non-xhr - submission prevented)</h4>
<form method="post" action="/regardless" target="_blank">
<fieldset>
<label>
<span>Subscribe</span>
<input type="email" name="email" required>
</label>
<input type="submit" value="Subscribe">
</fieldset>
</form>
<h1>Custom Validations</h1>
<h4>Show First Validation On Submit</h4>
<form method="post"
action-xhr="/form/echo-json/post"
target="_blank"
custom-validation-reporting="show-first-on-submit">
<fieldset>
<label>
<span>Your name</span>
<input type="text" name="name" id="name4" required pattern="\w+\s\w+">
<span visible-when-invalid="valueMissing" validation-for="name4"></span>
<span visible-when-invalid="patternMismatch" validation-for="name4">
Please enter your first and last name separated by a space (e.g. Jane Miller)
</span>
</label>
<label>
<span>Your email</span>
<input type="email" name="email" id="email4" required>
<span visible-when-invalid="valueMissing" validation-for="email4"></span>
<span visible-when-invalid="typeMismatch" validation-for="email4"></span>
</label>
<input type="submit" value="Subscribe">
</fieldset>
</form>
<h4>Show All Invalid Messages On Submit</h4>
<form method="post"
action-xhr="/form/echo-json/post"
target="_blank"
custom-validation-reporting="show-all-on-submit">
<fieldset>
<label>
<span>Your name</span>
<input type="text" name="name" id="name5" required pattern="\w+\s\w+">
<span visible-when-invalid="valueMissing" validation-for="name5"></span>
<span visible-when-invalid="patternMismatch" validation-for="name5">
Please enter your first and last name separated by a space (e.g. Jane Miller)
</span>
</label>
<label>
<span>Your email</span>
<input type="email" name="email" id="email5" required>
<span visible-when-invalid="valueMissing" validation-for="email5"></span>
<span visible-when-invalid="typeMismatch" validation-for="email5"></span>
</label>
<input type="submit" value="Subscribe">
</fieldset>
</form>
<h4>Show All Invalid Messages On Submit (Summary)</h4>
<form method="post"
action-xhr="/form/echo-json/post"
target="_blank"
custom-validation-reporting="show-all-on-submit">
<div>
<ol>
<li visible-when-invalid="valueMissing" validation-for="name51">
Name is a required field. Please fill it out.
</li>
<li visible-when-invalid="patternMismatch" validation-for="name51">
Please enter your first and last name separated by a space (e.g. Jane Miller)
</li>
<li visible-when-invalid="valueMissing" validation-for="email51">
We need your email to send you coolness!
</li>
<li visible-when-invalid="typeMismatch" validation-for="email51">
That doesn't look like an email. Please fix it.
</li>
</ol>
</div>
<fieldset>
<label>
<span>Your name</span>
<input type="text" name="name" id="name51" required pattern="\w+\s\w+">
</label>
<label>
<span>Your email</span>
<input type="email" name="email" id="email51" required>
</label>
<input type="submit" value="Subscribe">
</fieldset>
</form>
<h4>Show As You Go Messages On Submit</h4>
<form method="post"
action-xhr="/form/echo-json/post"
target="_blank"
custom-validation-reporting="as-you-go">
<fieldset>
<label>
<span>Your name</span>
<input type="text" name="name" id="name6" required pattern="\w+\s\w+">
<span visible-when-invalid="valueMissing" validation-for="name6"></span>
<span visible-when-invalid="patternMismatch" validation-for="name6">
Please enter your first and last name separated by a space (e.g. Jane Miller)
</span>
</label>
<label>
<span>Your email</span>
<input type="email" name="email" id="email6" required>
<span visible-when-invalid="valueMissing" validation-for="email6"></span>
<span visible-when-invalid="typeMismatch" validation-for="email6"></span>
</label>
<input type="submit" value="Subscribe">
</fieldset>
</form>
<h4 id="header1">on=change and form.submit examples</h4>
<button on="tap:header1.submit">Wrong Action - should throw an error</button>
<form method="post"
id="poll1"
action-xhr="/form/json/poll1"
target="_blank"
custom-validation-reporting="as-you-go">
<input name="clientId" type="hidden" value="CLIENT_ID(poll)" data-amp-replace="CLIENT_ID">
<input name="canonicalUrl" type="hidden" value="RANDOM and CANONICAL_URL" data-amp-replace="CANONICAL_URL RANDOM">
<fieldset>
<p>What is your favorite flightless bird?</p>
<ul>
<li>
<label>
<input name="question1" value="Penguins" type="radio" on="change:poll1.submit">
Penguins
</label>
</li>
<li>
<label>
<input name="question1" value="Ostriches" type="radio" on="change:poll1.submit">
Ostriches
</label>
</li>
<li>
<label>
<input name="question1" value="Kiwis" type="radio" on="change:poll1.submit">
Kiwis
</label>
</li>
<li>
<label>
<input name="question1" value="Wekas" type="radio" on="change:poll1.submit">
Wekas
</label>
</li>
</ul>
</fieldset>
<div submit-success>
<template type="amp-mustache">
Thanks for answering the poll! Here are the results!
<ul class="poll1-results">
{{#result}}
<li>
<div class="percentage-container {{answer}}">
{{#percentage}}<span class="one-pc"></span>{{/percentage}}
</div>
<span class="title">{{answer}}</span>
</li>
{{/result}}
</ul>
</template>
</div>
</form>
<h4 id="header1">Using button[type=submit]</h4>
<form method="post"
action-xhr="/form/echo-json/post"
target="_blank">
<input name="clientId" type="hidden" value="CLIENT_ID(poll)" data-amp-replace="CLIENT_ID">
<fieldset>
<label>
<span>Your name</span>
<input type="text" name="name" id="name1" required>
</label>
<label>
<span>Your email</span>
<input type="email" name="email" id="email1" required>
</label>
<input type="submit" value="Subscribe - Input">
<input type="submit" value="Subscribe 2 - Input">
<button type="submit">Subscribe with a Button</button>
<button type="submit">Subscribe with a Button 2</button>
</fieldset>
<div submit-success>
<template type="amp-mustache">
Success! Thanks {{name}} for subscribing! Please make sure to check your email {{email}}
to confirm!
</template>
</div>
<div submit-error>
<template type="amp-mustache">
Oops! {{name}}, We apologies something went wrong. Please try again later.
</template>
</div>
</form>
</body>
</html>