Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Client and server-side validation
Lecture: Simplified view methods
Login or purchase this course to watch this video and the rest of the course contents.
0:00 We saw that the real value of these view models is they let us write simpler view methods or action methods.
0:07 Here's our register story that we were working with. Notice how simple git is. No matter the data that has to be exchanged
0:13 like we have a country dropdown we got a populated country list out of our database. It doesn't matter. The view model is going to take care of that.
0:19 All we do is create the view model, send it along. For the post one, there's a little bit more going on and we got to
0:26 get the data from the form, do the validation and so on. But still, it's much simpler. So we're going to create this view model.
0:33 We're going to ask it to validate. And if it sets some error, well we need to show that error to the user. What data has to be exchanged?
0:39 It doesn't matter how big the form is or how complicated, the view model knows how to exchange that data. So we just say hey, send that data back.
0:46 Return vm.togit. And it takes care of all that. Now, if you make it through here all that rich validation you've added has passed.
0:54 You're probably good to go. So here we're going to say, if we were unable to register with their email and password
0:59 that the vm got ahold of, we might still need to send a message, so we're going to set like an error and send it back. Otherwise we just do a redirect
1:06 over to either / or /account. Wherever we want to send them to we've done it. Now isn't this clean and concise? Imagine how complicated a realistic
1:17 registration method here would be in actual code. Well, using view models, it doesn't get much worse than this.