Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Client and server-side validation
Lecture: Server-side validation with Viewmodels

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The second core service that view models provide to our application is server side validation.
0:06 So we've set all of these fields here in our initialization. This is the data exchange but sometimes, not all the time, sometimes
0:14 we need to validate it. If we're just getting, doing a standard get and showing the data, there's rarely a validation
0:20 but if we're working with forms in the post side of things then we do want to validate that. We could do that in the view method
0:27 but it's going to get all messy and why would we do it there? We're going to move it down to a validate method.
0:31 So here, we're going to say if the first name is not set or the last name is not set we'll give an error, like hey you have to specify a name
0:38 or an email is not right, you got to specify an email. Same for the password. We saw lots of interesting validation that we could do here.
0:45 You can imagine we could make this nicer, right? Regular expressions on emails and so on. We also could see that we did richer types of validation
0:53 not just here's a required field, but we go to the database and we say, someone's trying to register with this email to somebody already exists
1:01 who has registered with that email. Remember there's a uniqueness constraint and it's going to actually be some kind of error
1:06 if we try to save it theoretically and we want to give them a friendly message instead of a crash that says hey that didn't work for whatever reason.
1:13 So we can tell them really nicely hey that user with that email already exists. And even suggest, would you like to try to log in
1:19 or reset your password? If you're trying to register, maybe you forgot what your account is. Things like that.
1:23 So there's validation, it's really, really nice. What's cool about it is, we can add tons of validation here
1:29 and it doesn't make our view method any more complicated. This validate method just bundles it all up and it's great, right here.

Talk Python's Mastodon Michael Kennedy's Mastodon