Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Client and server-side validation
Lecture: Concept: Viewmodel
Login or purchase this course to watch this video and the rest of the course contents.
0:00 You've seen view models in action let's review them as a general concept. So here's a mock up of a web browser
0:09 some sort of architectural view of our server and the user of the web browser submitting a form. Here they're doing their HTTP POST
0:16 for the register action form page. And they're submitting some form data which we didn't elaborate too much on here
0:24 but you see some sort of form data is being submitted. Now, the way that Flask encourages you to handle this
0:30 in general, and a lot of the web framers do is to just have some super complicated view method. Why? Well, there's no other pattern to tell you
0:42 it doesn't have to be that way cause you have to do all the data acquisition the data normalization like stripping
0:48 and lower-casing the email, stuff like that. You've got to do the validation you've got to do lots of testing like does this user already exist?
0:56 Typically, here's the function where you do that. Go to town, sorry it's 200 lines. All right, things like that. But it doesn't have to be that way.
1:03 We saw the view model pattern takes the data exchange the data normalization, and the data validation and it pushes it somewhere else
1:11 that can be tested separately, maintained separately. And our view method only has to process the essence. It gets the data, maybe it creates the user
1:20 and it doesn't redirect, or something to that effect. Sends them a welcome email, things like this. So we can exchange this yellow box
1:26 for a much smaller, more maintainable version and instead of writing all that validation and exchange data, we create a view model
1:33 and we just work with that view model over here. Now this is the general idea of this view model design pattern.