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