Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Client and server-side validation
Lecture: The motivation for viewmodels

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In the previous chapter we worked with HTML forums
0:02 and user input and exchanging data
0:04 between the view methods and the templates.
0:07 And all that worked well
0:09 but it wasn't that pretty
0:11 and we were actually skipping over a lot of stuff.
0:14 There were certain validations I didn't add
0:16 or were not fine-grain enough checks on the server side
0:19 because it was going to just blow up the view method
0:21 with mostly validation, mostly checking
0:23 and then a little bit of work
0:24 and it would have taken away from what we were trying to do.
0:27 But in real apps you don't omit validation
0:29 just because, Hey, it's going to make this method messy.
0:32 No, you've got to actually put it in there.
0:34 So what we're going to do is
0:35 we're going to look at a design pattern
0:37 that is not promoted by Flask or special to Flask
0:41 can be used with other web frameworks
0:43 called View Models.
0:44 Now this is just something I'm adapting to Flask
0:47 and I think is super, super valuable.
0:49 I'm going to make a case for that
0:50 as we go through this chapter
0:52 I think most of you will agree as well
0:54 but it's one of these patterns
0:55 that helps with organization
0:57 you can use it or not use it.
0:58 Now I've already moved our data
1:00 over into chapter 13 validation
1:02 as you can see up here at the top
1:04 so I've made a copy of what we had
1:07 in chapter 12 with the forms
1:08 and this is starting with exactly that code
1:10 and of course there's a starter version as well.
1:12 We're going to evolve this to be better.
1:14 So let's go over here and just look at our views
1:18 and account one is probably
1:20 the best place to start to highlight this.
1:22 So even over here when we're just
1:24 trying to show the index page
1:26 let me remind you what that looks like.
1:28 Over here we can login
1:30 we have to pass a couple of things across.
1:32 We have to pass what the account is
1:34 and whether or not they're logged in or logged out.
1:38 That's used by the shared layout
1:39 and this is used by the individual template.
1:42 So we've got to pass that along here
1:44 and there's a couple of things we're doing.
1:45 We're checking to see that there's a user here
1:48 and we have to pass both the user
1:50 and the user id class across.
1:52 But this is a simple one
1:53 let's look at something more complex.
1:55 So down here we have this post.
1:58 Now what we have to do is we have to
1:59 actually get data from the form
2:01 we have to validate it.
2:04 If it's not valid we have to send a ton of information
2:07 we have to round-trip their data they've entered
2:10 we have to send an error message and the user_id.
2:13 The user_id is used by the outer container template
2:15 and this is used by the form.
2:18 Again we're going to do some more work
2:20 do some more validation and so on.
2:22 So this is really not fun
2:24 this is error-prone code
2:25 and we should be actually doing more checks
2:27 like instead of just if there's no name or email
2:30 we should also do a strip on the name and email
2:33 so space doesn't work
2:34 we should have different error messages for name and email
2:37 like, Your name is required
2:38 The email is required
2:39 not just like, Hey, stuff's missing!
2:41 That's always frustrating.
2:43 So we're going to see that while this is a little bit messy
2:45 it can be massively simplified with view models.