RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Validation with view models
Lecture: View model classes

Login or purchase this course to watch this video and the rest of the course contents.
0:01 I'd be pretty happy to call this view model done
0:04 but in a real system, we'll have many of these view models for different situations,
0:08 there might be an update auto view model
0:11 that's different than a create auto view model
0:13 and of course maybe we want to create users and dealerships
0:16 and like there is just a lot of stuff in practice
0:19 where you probably end up with more than one of these,
0:21 maybe more than ten these,
0:23 so let's do a little bit of work to make this easier to add new view models;
0:26 so over here, I'm going to add the name will give it all away a base view model.
0:30 So we're just going to have a class called view model base let's say,
0:36 I will just put an empty representation for a minute
0:41 we'll come over here and say you know what, this thing is going to be one of those
0:44 and let's go ahead and call the base initializer, and things like this
0:50 like the errors, pretty much any view model that is worth having
0:57 is going to do some kind of creation and error checking and validation,
1:01 there's probably a few that don't, but the majority of them will
1:04 so let's go ahead and give it, like push the concept of errors
1:07 and handling error messages down to the base
1:10 all right, so I got that one and then this for now, there are errors
1:14 with your request or something like that
1:18 make this little general, and then this can go down there as well;
1:21 great, so now any view model we want
1:24 automatically knows how to keep track of its errors
1:26 and report all the errors that it has encountered.
1:28 So now it's really just down to the car stuff,
1:31 getting the values we need, validating them,
1:34 I guess we probably could leave that empty ;
1:39 But just take the default and we'll put the values in here,
1:42 test them and of course don't create the car if it's not possible or valid,
1:49 let's just test one more time that our new base class here is doing this job.
1:54 Okay, so maybe we want to omit the name, try to create one,
1:59 no, your name is required, so obviously we want the errors field to store it in,
2:03 and the errors message worked, so we put this back
2:06 do it again, now we've created a new car,
2:09 perfect, so I think we're done with our view model section.