Building data-driven web apps with Pyramid and SQLAlchemy Transcripts
Chapter: Client and server-side validation
Lecture: Viewmodel validation
0:00 Saw the one job of these view models
0:03 and that's to do the data exchange.
0:04 The other is validation.
0:06 So, let's look at that now.
0:08 Here we have our gathering of data, right.
0:12 We're creating this view model based on a request
0:14 and it's populating even normalizing some of our data
0:17 like first name, last name, and email.
0:19 Great but on the POST side of things
0:22 where we're taking this data in
0:24 and we're doing a operation based on
0:26 assuming that we have good data
0:28 like that they actually set the name
0:29 they set the email and so on.
0:31 We want to do validation.
0:33 So that's where this error thing comes in.
0:36 So we're going to do this validate
0:38 and we're going to say check the first name or last name
0:41 and if either of them are empty
0:42 we'll say you must specify your full name.
0:45 If there's no email you must specify an email address.
0:48 If you don't specify a password similarly
0:51 you get a message that says that.
0:54 So, we set these errors and then the
0:56 action method or the view method
0:58 its job will be to make sure that there's no errors
1:01 before it actually does its operation.
1:04 We can even do things that interact with the database.
1:08 So, here this is a registration operation.
1:10 We did basic validation on the form values.
1:13 First name, last name exist and are not empty.
1:16 Email is set, we could do regular expression
1:19 to say this is a valid email
1:20 or something simpler like check for @ and.
1:24 A few things like that.
1:25 But here we're actually going alright
1:27 once you pass all the basic validation
1:29 let's go to the database and see if there
1:31 is a user with the email address
1:33 that you're trying to register as
1:35 because if there is we're not going to let you do that.
1:38 The user already exists, right.
1:40 So, we have these sort of basic validation
1:42 and higher level validation we can do.