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
0:02 is they let us write simpler view methods
0:04 or action methods.
0:06 Here's our register story that we were working with.
0:09 Notice how simple git is.
0:10 No matter the data that has to be exchanged
0:12 like we have a country dropdown
0:13 we got a populated country list
0:15 out of our database.
0:16 It doesn't matter.
0:17 The view model is going to take care of that.
0:18 All we do is create the view model, send it along.
0:22 For the post one, there's a little
0:24 bit more going on and we got to
0:25 get the data from the form, do the validation
0:28 and so on. But still, it's much simpler.
0:29 So we're going to create this view model.
0:32 We're going to ask it to validate.
0:33 And if it sets some error, well
0:35 we need to show that error to the user.
0:37 What data has to be exchanged?
0:38 It doesn't matter how big the form is
0:40 or how complicated, the view model
0:42 knows how to exchange that data.
0:43 So we just say hey, send that data back.
0:45 Return vm.togit.
0:47 And it takes care of all that.
0:49 Now, if you make it through here
0:50 all that rich validation you've
0:52 added has passed.
0:53 You're probably good to go.
0:54 So here we're going to say, if we were unable
0:56 to register with their email and password
0:58 that the vm got ahold of, we might still
1:00 need to send a message, so we're going to set
1:01 like an error and send it back.
1:03 Otherwise we just do a redirect
1:05 over to either / or /account.
1:08 Wherever we want to send them to we've done it.
1:11 Now isn't this clean and concise?
1:13 Imagine how complicated a realistic
1:16 registration method here would be
1:18 in actual code.
1:20 Well, using view models, it doesn't get
1:22 much worse than this.