/
index.html
412 lines (259 loc) · 20 KB
/
index.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
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
<meta charset="utf-8">
<title>Crash Course in Web2py (Part 3 - Form Validation) - Michael Herman</title>
<meta name="author" content="Michael Herman">
<meta name="description" content="Well, in the last tutorial I showed you how to create a basic web form that stores the submitted data in a SQLite database. In this tutorial, we& …">
<!-- http://t.co/dKP3o1e -->
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="canonical" href="http://mherman.org/blog/2012/12/06/crash-course-in-web2py-part-3-form-validation">
<link href="/favicon.png" rel="icon">
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
<link href="/atom.xml" rel="alternate" title="Michael Herman" type="application/atom+xml">
<script src="/javascripts/modernizr-2.0.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="/javascripts/libs/jquery.min.js"%3E%3C/script%3E'))</script>
<script src="/javascripts/octopress.js" type="text/javascript"></script>
<!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
<link href="//fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
<link href="//fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet" type="text/css">
<link href="//fonts.googleapis.com/css?family=Fjalla+One" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">
<!--- MathJax Configuration -->
<script type="text/javascript" src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-37074204-1', 'auto');
ga('send', 'pageview');
</script>
</head>
<body class="collapse-sidebar sidebar-footer" >
<header role="banner"><hgroup>
<h1><a href="/">Michael Herman</a></h1>
<h2>Software Developer</h2>
</hgroup>
</header>
<nav role="navigation"><ul class="subscribe" data-subscription="rss">
<li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS" target="_blank"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="25" height="25" viewbox="0 0 100 100"><path class="social" d="M 13.310204,73.332654 C 5.967347,73.332654 0,79.322448 0,86.621428 c 0,7.338776 5.967347,13.262246 13.310204,13.262246 7.370408,0 13.328572,-5.92245 13.328572,-13.262246 0,-7.29898 -5.958164,-13.288774 -13.328572,-13.288774 z M 0.01530612,33.978572 V 53.143878 C 12.493878,53.143878 24.229592,58.02347 33.068368,66.865306 41.894898,75.685714 46.767346,87.47449 46.767346,100 h 19.25 C 66.017346,63.592858 36.4,33.979592 0.01530612,33.978572 l 0,0 z M 0.03877552,0 V 19.17449 C 44.54796,19.17551 80.77551,55.437756 80.77551,100 H 100 C 100,44.87653 55.15102,0 0.03877552,0 z"></path></svg></a></li>
</ul>
<form action="http://google.com/search" method="get">
<fieldset role="search">
<input type="hidden" name="sitesearch" value="mherman.org" />
<input class="search" type="text" name="q" results="0" placeholder="Search"/>
</fieldset>
</form>
<ul class="main-navigation">
<li><a href="/">Blog</a></li>
<li><a href="/about">About</a></li>
</ul>
</nav>
<div id="main">
<div id="content">
<div>
<article class="hentry" role="article">
<header>
<h1 class="entry-title">Crash Course in Web2py (Part 3 - Form Validation)</h1>
<p class="meta">
<time class='entry-date' datetime='2012-12-06T21:52:00-07:00'><span class='date'><span class='date-month'>Dec</span> <span class='date-day'>6</span><span class='date-suffix'>th</span>, <span class='date-year'>2012</span></span> <span class='time'>9:52 pm</span></time>
</p>
</header>
<div class="entry-content"><p>Well, in the last <a href="http://mherman.org/blog/2012/12/01/crash-course-in-web2py-part-2-web-forms/">tutorial</a> I showed you how to create a basic web form that stores the submitted data in a SQLite database. In this tutorial, we’re going to add in data validation to your form.</p>
<p><em>Assumptions:</em></p>
<ol>
<li>You <a href="http://mherman.org/blog/2012/11/27/crash-course-in-web2py-part-1/">installed</a> web2py</li>
<li>You created the skeleton web form from <a href="http://mherman.org/blog/2012/12/01/crash-course-in-web2py-part-2-web-forms/#.U5btEpRdUZ0">Part 2</a></li>
</ol>
<p>Start the web2py server, go to the admin interface, and then edit the “form” application (or whatever you decided to name it).</p>
<h2>Model</h2>
<p>Right now, there is a requirement set in the db.py file for each field-</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">requires</span><span class="o">=</span><span class="n">IS_NOT_EMPTY</span>
</span></code></pre></td></tr></table></div></figure>
<p>In other words - all fields must be filled out or an error populates.</p>
<h2>View</h2>
<p>What do you want to display to the user?</p>
<p>Open up display_your_form.html and change your code to match the following:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'>{{extend 'layout.html'}}
</span><span class='line'><span class="nt"><br</span> <span class="nt">/><br</span> <span class="nt">/><br</span> <span class="nt">/></span>
</span><span class='line'><span class="nt"><h1></span>Web Form<span class="nt"></h1></span>
</span><span class='line'><span class="nt"><br</span> <span class="nt">/></span>
</span><span class='line'><span class="nt"><p></span>Please enter your first and last names, and email address.<span class="nt"><br</span> <span class="nt">/></span>
</span><span class='line'>Please note: All attempts must be error free before any info is accepted.<span class="nt"></p></span>
</span><span class='line'><span class="nt"><h2></span>Inputs:<span class="nt"></h2></span>
</span><span class='line'>{{=form}}
</span><span class='line'><span class="nt"><h2></span>Submitted Fields:<span class="nt"></h2></span>
</span><span class='line'>{{=BEAUTIFY(request.vars)}}
</span><span class='line'><span class="nt"><h2></span>Accepted Fields:<span class="nt"></h2></span>
</span><span class='line'>{{=BEAUTIFY(form.vars)}}
</span><span class='line'><span class="nt"><h2></span>Errors:<span class="nt"></h2></span>
</span><span class='line'>{{=BEAUTIFY(form.errors)}}
</span></code></pre></td></tr></table></div></figure>
<p>By adding <em>{ {extend ‘layout.html’}}</em>, you will be able to display the flash error messages, which we’ll add in next.</p>
<h2>Controller</h2>
<p>Update deafult.py to match the following code-</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">def</span> <span class="nf">display_your_form</span><span class="p">():</span>
</span><span class='line'> <span class="n">form</span> <span class="o">=</span> <span class="n">SQLFORM</span><span class="p">(</span><span class="n">db</span><span class="o">.</span><span class="n">register</span><span class="p">)</span>
</span><span class='line'> <span class="k">if</span> <span class="n">form</span><span class="o">.</span><span class="n">accepts</span><span class="p">(</span><span class="n">request</span><span class="p">,</span><span class="n">session</span><span class="p">):</span>
</span><span class='line'> <span class="n">response</span><span class="o">.</span><span class="n">flash</span> <span class="o">=</span> <span class="s">'Thanks! The form has been submitted.'</span>
</span><span class='line'> <span class="k">elif</span> <span class="n">form</span><span class="o">.</span><span class="n">errors</span><span class="p">:</span>
</span><span class='line'> <span class="n">response</span><span class="o">.</span><span class="n">flash</span> <span class="o">=</span> <span class="s">'Please correct the error(s).'</span>
</span><span class='line'> <span class="k">else</span><span class="p">:</span>
</span><span class='line'> <span class="n">response</span><span class="o">.</span><span class="n">flash</span> <span class="o">=</span> <span class="s">'Try again - no fields can be empty.'</span>
</span><span class='line'> <span class="k">return</span> <span class="nb">dict</span><span class="p">(</span><span class="n">form</span><span class="o">=</span><span class="n">form</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
<p>This adds an If Statement to display text based on whether the user submits your form with the required fields or not.</p>
<h2>Test</h2>
<ol>
<li>Go to <a href="http://127.0.0.1:8000/form/default/display_your_form.html">http://127.0.0.1:8000/form/default/display_your_form.html</a>.</li>
<li>Enter your first name, last name, and email.</li>
<li>Your output should look similar to this-</li>
</ol>
<p><img src="http://www.backwardsteps.com/uploads/2012-12-05_0954.png" alt="" /></p>
<p>As long as no fields are blank, you won’t see any errors - and the data will be added to the database. Notice how you can now see the unique identifier. The message “Thanks! The form has been submitted.” is visible in the top right corner, as well.</p>
<p>How does this work?</p>
<p>Well, when a field is submitted, it’s filtered through the <em>accepts</em> method from the Controller, according to the requirements specified in the database schema (<em>IS_NOT_EMPTY)</em>. If the field meets the requirement (accepts returns <em>True)</em>, it’s passed to <em>form.vars</em>; if not (accepts returns <em>False)</em>, an error populates, which is then stored in <em>form.errors</em>.</p>
<p>Got it?</p>
<p>See what happens when you enter an error.</p>
<p><img src="http://www.backwardsteps.com/uploads/2012-12-05_1001.png" alt="" /></p>
<p>So when Han forgets his last name and just decides to leave it blank, the field values that meet the requirement are still passed. But they are not added to the database, since there is an error. And you should see an error message in the top right corner. As soon as he remembers, he can go back and correct. Poor Han.</p>
<p>What happens though if he gets confused and enters his first name correctly, but enters his last name in the email field and his email in the last name field? Try it. You should not see an error.</p>
<p>This is a problem. Let’s add additional requirements to prevent this from happening.</p>
<h2>Model</h2>
<p>Update the code in db.py to match the following-</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">db</span> <span class="o">=</span> <span class="n">DAL</span><span class="p">(</span><span class="s">'sqlite://webform.sqlite'</span><span class="p">)</span>
</span><span class='line'><span class="n">db</span><span class="o">.</span><span class="n">define_table</span><span class="p">(</span><span class="s">'register'</span><span class="p">,</span>
</span><span class='line'> <span class="n">Field</span><span class="p">(</span><span class="s">'first_name'</span><span class="p">,</span> <span class="n">requires</span><span class="o">=</span><span class="p">[</span><span class="n">IS_NOT_EMPTY</span><span class="p">(),</span> <span class="n">IS_ALPHANUMERIC</span><span class="p">()]),</span>
</span><span class='line'> <span class="n">Field</span><span class="p">(</span><span class="s">'last_name'</span><span class="p">,</span> <span class="n">requires</span><span class="o">=</span><span class="p">[</span><span class="n">IS_NOT_EMPTY</span><span class="p">(),</span> <span class="n">IS_ALPHANUMERIC</span><span class="p">()]),</span>
</span><span class='line'> <span class="n">Field</span><span class="p">(</span><span class="s">'email'</span><span class="p">,</span> <span class="n">requires</span><span class="o">=</span><span class="p">[</span><span class="n">IS_NOT_EMPTY</span><span class="p">(),</span> <span class="n">IS_EMAIL</span><span class="p">()]))</span>
</span></code></pre></td></tr></table></div></figure>
<ul>
<li><em>IS_ALPHANUMERIC()</em> requires that the field can only contain characters ranging from a-z, A-Z, or 0-9</li>
<li><em>IS_EMAIL</em> requires that the field value must look like an email address</li>
</ul>
<p>Now watch happens when you switch the last name and email address.</p>
<p><img src="http://www.backwardsteps.com/uploads/2012-12-05_1007.png" alt="" /></p>
<p>Good. Now he just needs to enter the correct value in the correct fields.</p>
<p>Hey - at least he remembered his last name.</p>
<h2>Recap</h2>
<p>By just tweaking the code a bit, we added basic validation to ensure that no fields are empty and to limit any data integrity issues. Again - Do not deploy the app on PythonAnywhere just yet. We still have more features to add, but we are well underway on having the best form on the Internet.</p>
<p>You can find the new code for db.py, display_your_form.html, and default.py <a href="https://github.com/mjhea0/web2py/tree/master/form%20-%20part%202">here</a>. When ready, move on to <a href="http://mherman.org/blog/2012/12/09/crash-course-in-web2py-part-4-managing-form-records/#.U5bvg5RdUZ0">Part 4</a>.</p>
</div>
<footer>
<p class="meta">
<span class="byline author vcard">Authored by <span class="fn">
Michael Herman
</span></span>
<time class='entry-date' datetime='2012-12-06T21:52:00-07:00'><span class='date'><span class='date-month'>Dec</span> <span class='date-day'>6</span><span class='date-suffix'>th</span>, <span class='date-year'>2012</span></span> <span class='time'>9:52 pm</span></time>
<span class="categories">
<a class='category' href='/blog/categories/python/'>python</a>
</span>
</p>
<div class="sharing">
<!--
<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://mherman.org/blog/2012/12/06/crash-course-in-web2py-part-3-form-validation/" data-via="" data-counturl="http://mherman.org/blog/2012/12/06/crash-course-in-web2py-part-3-form-validation/" >Tweet</a>
<div class="fb-like" data-send="true" data-width="450" data-show-faces="false"></div>
-->
<br>
<!-- AddThis Button BEGIN -->
<div class="addthis_toolbox addthis_default_style addthis_32x32_style">
<a class="addthis_button_preferred_1"></a>
<a class="addthis_button_preferred_2"></a>
<a class="addthis_button_preferred_3"></a>
<a class="addthis_button_preferred_4"></a>
<a class="addthis_button_compact"></a>
<a class="addthis_counter addthis_bubble_style"></a>
</div>
<script type="text/javascript">var addthis_config = {"data_track_addressbar":true};</script>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-50e5f1cc35ad077d"></script>
<!-- AddThis Button END -->
<br>
</div>
<p class="meta">
<a class="basic-alignment left" href="/blog/2012/12/01/crash-course-in-web2py-part-2-web-forms/" title="Previous Post: Crash Course in web2py (part 2 - web forms)">« Crash Course in web2py (part 2 - web forms)</a>
<a class="basic-alignment right" href="/blog/2012/12/09/crash-course-in-web2py-part-4-managing-form-records/" title="Next Post: Crash Course in web2py (part 4 - managing form records)">Crash Course in web2py (part 4 - managing form records) »</a>
</p>
</footer>
</article>
<section>
<h1>Comments</h1>
<div id="disqus_thread" aria-live="polite"><noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
</div>
</section>
</div>
</div>
</div>
<footer role="contentinfo"><div>
<span>Copyright © 2017 - Michael Herman</span><br>
<a href="https://github.com/mjhea0"><i class="fa fa-github-square" style="font-size:2em" aria-hidden="true"></i></a>
<a href="https://twitter.com/mikeherman"><i class="fa fa-twitter-square" style="font-size:2em" aria-hidden="true"></i></a>
<a href="http://www.linkedin.com/pub/michael-herman/3b/a94/4"><i class="fa fa-linkedin-square" style="font-size:2em" aria-hidden="true"></i></a>
<a href="http://www.youtube.com/hermanmu"><i class="fa fa-youtube-square" style="font-size:2em" aria-hidden="true"></i></a>
</div>
</footer>
<script type="text/javascript">
var disqus_shortname = 'michaelherman';
// var disqus_developer = 1;
var disqus_identifier = 'http://mherman.org/blog/2012/12/06/crash-course-in-web2py-part-3-form-validation/';
var disqus_url = 'http://mherman.org/blog/2012/12/06/crash-course-in-web2py-part-3-form-validation/';
var disqus_script = 'embed.js';
(function () {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/' + disqus_script;
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
}());
</script>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js#appId=212934732101925&xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<script type="text/javascript">
(function(){
var twitterWidgets = document.createElement('script');
twitterWidgets.type = 'text/javascript';
twitterWidgets.async = true;
twitterWidgets.src = '//platform.twitter.com/widgets.js';
document.getElementsByTagName('head')[0].appendChild(twitterWidgets);
})();
</script>
</body>
</html>