Skip to content

ASPNET MVC Bootstrap Responsive Web Programming Lecture 9 Custom and Client Side Validation

FurkanGozukara edited this page Oct 18, 2026 · 1 revision

ASP.NET - MVC - Bootstrap - Responsive Web Programming, Lecture 9: Custom and Client Side Validation

ASP.NET - MVC - Bootstrap - Responsive Web Programming, Lecture 9: Custom and Client Side Validation

image Hits Patreon BuyMeACoffee Furkan Gözükara Medium Codio Furkan Gözükara Medium

YouTube Channel Furkan Gözükara LinkedIn Udemy Twitter Follow Furkan Gözükara

#SoftwareDevelopment #Software #Engineering #CSharp #Coding #Course

Asp.NET Core V5 - MVC Pattern - Bootstrap V5 - Responsive Web #Programming with C# Full Course Playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffsnAWtgk4ja3HN3xgMKF7BOE

GitHub repository of the course : https://github.com/FurkanGozukara/Responsive-Web-Design-With-ASP.NET-5-and-MVC-Pattern-and-BootStrap-5-2021

Discord channel link of the course : https://discord.gg/6Mrb8MwteQ

How to use Discord : https://youtu.be/AEwPtYiLvsQ

In Lecture 9:

  • How to change namespace of the project properly

  • How to implement a model specific custom validation with IValidatableObject - Validate - ValidationContext - ValidationResult and yield return

  • What is yield return and how to use it

  • How to add client side validation automatically with jQuery validation and prevent miss-input postback to the server

  • How to auto generate form for model attributes with using asp tag helpers

  • How to enable asp tag helpers in ASP.NET MVC Core Views

  • How to use your MVC model directly in an another application with DLL referencing

  • How to manually validate your MVC model through Validator.TryValidateObject

last page : https://asp.mvc-tutorial.com/layout/introduction/

Please subscribe and make comments.

This course requires fundamental knowledge about programming and C#. So please watch our below playlists related to C#.

Our courses playlists are like below:

[1] Introduction to Programming Full Course with C# playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffskoSXySh0MdiayPJsBZ7m2o

[2] Advanced #Programming with C# Full Course Playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffslHaBdS3RUW26RKzSjkl8m4

[3] Object Oriented Programming Full Course with C# playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffsnH3XJb66FDIHh1yHwWC26I

[4] Asp.NET Core V5 - MVC Pattern - Bootstrap V5 - Responsive Web #Programming with C# Full Course Playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffsnAWtgk4ja3HN3xgMKF7BOE

[5] Artificial Intelligence (AI) and Machine Learning (ML) Full Course with C# Examples playlist: https://www.youtube.com/playlist?list=PL_pbwdIyffskVschrADCL6KEnL_nqDtgD

[6] Software Engineering Full Course playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffslgxMVyXhnHiSn_EWTvx1G-

[7] Security of Information Systems Full Course playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffslM_o92NwkaUzD7C6Fekx26

[8] (Turkish) Bilgisayar Becerileri Tam Ders playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffsmyE2e909ea1MXLcMb8MenG

auto subtitle timing : NA

Video Transcription

  • 00:00:01 Hello dear students, welcome to the lecture 9 of responsive web design with asp.net 5

  • 00:00:07 and MVC pattern and bootstrap 5. So today, we will continue from custom model validation.

  • 00:00:14 And if you remember, we were left here. So let's start with cloning our previous lectures

  • 00:00:22 project.

  • 00:00:25 As usual.

  • 00:00:29 Today, I will also show you how to change.

  • 00:00:40 Namespace of the entire project. So we will also learn how to do that. It is a bit tricky

  • 00:00:48 and therefore we need to be careful.

  • 00:00:57 Okay, first start with renaming options, that whatever we are. We can rename use

  • 00:01:15 Okay, let's see. Them solution as well.

  • 00:01:21 Okay, however, we still need to rename our

  • 00:01:28 namespace. Therefore, you see, I have to find all lecture 6 dots and change it to in lecture

  • 00:01:37 9 like this. Okay, replace all

  • 00:01:42 Okay, 17 occurrences and still not enough. We also need to change if there are other

  • 00:01:59 names faces,

  • 00:02:03 Okay, like this.

  • 00:02:06 Okay.

  • 00:02:08 Okay, six more. And then we need to check properties.

  • 00:02:23 And said, default namespace as nine? Okay. Are we still don't know, probably, but let's

  • 00:02:30 try. There is probably also see CS Crush file that we need to change. I just try to run

  • 00:02:41 it because I wanted to save all changes

  • 00:02:44 Okay.

  • 00:02:52 Let's open the Cs plush pile years.

  • 00:03:12 And you see oh it is already renamed it. Nice.

  • 00:03:20 Okay, so it should work. I believe. Yes it is working. Okay so if you remember we were

  • 00:03:27 doing custom validation in the previous lecture which we had coded in lecture, eight class

  • 00:03:36 here, you see? It was inheriting, validation attribute and we were overriding the validation

  • 00:03:43 results and it was behaving according to ours.

  • 00:03:47 Rules as we expect. Now, we will continue with custom validation with, I validate object

  • 00:03:57 if I remember correctly.

  • 00:04:02 I think it is written here. Yes. Okay let's start reading.

  • 00:04:09 Custom validation with I validate, herbal object as an alternative to the approach.

  • 00:04:14 We just saw where we implemented a custom-made version of the validation attribute. We can

  • 00:04:19 make our model. Implement the I validate double object-- interface by doing so we can contain

  • 00:04:25 the logic needed for custom validation directly on the model class. Instead of implementing

  • 00:04:30 a new class, this makes even more sense. If the validation logic, you're looking to add

  • 00:04:35 concerns more than one property. Since the validation is now no longer.

  • 00:04:39 Longer tied to a single specific property.

  • 00:04:42 The I validate double object-- interface only has a single method validate. It should contain

  • 00:04:48 all the logic you need. That can't be applied to your properties through the built-in mechanisms.

  • 00:04:52 So in other words, you are still free to combine both regular and custom validation. We have

  • 00:04:58 done just that in this new class called Web user validate able. But please notice that

  • 00:05:02 the class name doesn't have to include the word valid data below or anything like that,

  • 00:05:06 you are still free to call it whatever you want. Okay. I think to implement this, I will.

  • 00:05:12 Generate lecture 9. Okay, let's start with adding our model as class.

  • 00:05:23 To the simple empty class lecture 9.

  • 00:05:27 Okay, and then let's add our weave.

  • 00:05:35 Okay, in new folder lecture 9.

  • 00:05:43 Inside lecture 9. Let's add index or custom name is one. We can do that. Let's do it.

  • 00:05:51 Let's call it as lecture nine leave. We can do as we want, it's totally fine. And we also

  • 00:06:00 need a controller for that and it will be

  • 00:06:08 Names as lecture 9 controllers.

  • 00:06:17 Okay.

  • 00:06:19 So, I will take the logic in lecture eight.

  • 00:06:26 And I will modify it. So in lecture eight class. This was our logic. I mean, the model.

  • 00:06:36 Not the music.

  • 00:06:38 Okay here. And now this class is going to inherit. I validate object.

  • 00:06:48 Like this. So I need to add the necessary references which are okay, data and annotations

  • 00:06:58 And okay, so we still have a problem.

  • 00:07:09 Why is it not?

  • 00:07:14 Working, I wanted object.

  • 00:07:20 Okay, provides a way for an object to be validated. Okay. So what is the lecture? 9 does not Implement

  • 00:07:31 interface members? Okay so we need to implement members or validate because since this is

  • 00:07:39 an interface this is not a regular class. We have to quote interface and interface is

  • 00:07:47 this one probably guy, element label, validation result validate.

  • 00:07:52 Okay, so I will add it to here. So this is the validate interface which this in validated

  • 00:08:03 methods which this interface was expecting. And inside this validate, I can write whatever

  • 00:08:11 I want, okay? And so,

  • 00:08:17 I will add some custom messages here. For example, you see these birthday, it says,

  • 00:08:22 and our birthday is set as DT birthday. Therefore, let's change it. You know, that this means

  • 00:08:30 the properties or at Fields inside this class.

  • 00:08:38 and so, it returns

  • 00:08:41 Okay. This surely you are not that old and it's returns, NIV birthday.

  • 00:08:50 Okay, so

  • 00:08:53 What is the second? Okay, so the second parameter is the member name. And so, this member name

  • 00:09:02 will be equal to R parameter name.

  • 00:09:04 All right, all right, because it is returning here to return need. So it is using yield

  • 00:09:10 and yield is a bit

  • 00:09:16 Different things. So let me show you.

  • 00:09:29 Okay.

  • 00:09:33 Let's understand yield to return, it will be useful for your future as well.

  • 00:09:45 Once you understand having yield return in c-sharp code, you will not help wondering

  • 00:09:49 what a return form or a special mechanism to handle. It is in the article below, we

  • 00:09:55 will learn some programming techniques that revolve around C sharp yield return. Key words,

  • 00:10:00 quote from msdn, the yield keyword signals to the compiler that the method in which it

  • 00:10:05 appears is an iterator block. The compiler, generates a class to implement the behavior

  • 00:10:11 that is expressed in the iterator block.

  • 00:10:13 In the iterator block, the yield keyword is used together with the return keyword to provide

  • 00:10:18 a value to the enumerator object. This is the value that is returned. For example, in

  • 00:10:24 each Loop of a foreach statement, the yield keyword is also used with break to Signal

  • 00:10:29 the end of iteration. Just so here's this return will not return from the methods immediately

  • 00:10:39 it is used as a iterator

  • 00:10:44 So it will return every case it will check every case and it will continue to iteration

  • 00:10:52 you see. It is returning and I innumerable, but if it was returning only this one, it

  • 00:10:58 wouldn't be I and admirable so it can return multiple results. And here, this DT birthday

  • 00:11:07 is defining. This result is, let's say,

  • 00:11:13 You belong to which element of validated and it is belong to this one. Okay. So I don't

  • 00:11:21 need these anymore because it will be automatically validated. Here's

  • 00:11:28 Probably, so let's continue.

  • 00:11:34 Iteration one method of operation of yield when combined with the return mechanism allowing

  • 00:11:39 creation the function, returns a value that handles certain information and the loop and

  • 00:11:43 puts an end to certain conditions before it, this is contrary to the case of a normal return.

  • 00:11:49 I get a return value of the function that will return and finish the function at that

  • 00:11:54 point. But here the function is to call back in the loop. Containing, the yield return

  • 00:11:58 function that just ended

  • 00:12:00 From here, we can imagine an action that is rather abstract but can be useful in some

  • 00:12:05 cases to help keep code concise and tight, to some requirements for the use of yield

  • 00:12:10 return, do not put yield in an unsafe block. Do not use ref or out keywords with the parameters

  • 00:12:17 of the method, operator or accessor get her / Setter yield return can only be placed in

  • 00:12:23 the try block, if it is followed by a finally block.

  • 00:12:27 Yield break can be put in the try block and catch but not placed in the finally block.

  • 00:12:33 Do not use yield of the anonymous method. Okay. So here's

  • 00:12:37 yeah, it is not encapsulated with anything so it can be used this way as well. So these

  • 00:12:44 are the useful practices but not necessarily three. An example of using yield return

  • 00:12:53 To use yield return, you just need to create a method with a return type that is an ienumerable

  • 00:12:59 arrays and collections in net, implements ienumerable interface with a loop and use

  • 00:13:04 yield return to return a value to set in the loop body.

  • 00:13:09 Some examples are of a function with two arguments in start in number made in even numbers starting

  • 00:13:14 from the starting number.

  • 00:13:17 The following is the code written in the usual. Way transmitted to the in Returns, the result

  • 00:13:21 is an array. And then use one array to receive. The value returned he saw. This is the traditional

  • 00:13:27 way. Probably, let's open it.

  • 00:13:29 Okay, when we zoom in, we see that there is a function which takes two parameters and

  • 00:13:38 this function defines a new array with the number second number and it's makes a loop

  • 00:13:49 and calculate the error like this and which on that array done. This already is printed

  • 00:13:57 to the sequin like this, okay?

  • 00:14:00 Now, let's see if it's real sweet return. So this year's return

  • 00:14:05 You see we can iterate this function like this and even though this is returning, it

  • 00:14:16 doesn't break these for love so it continued for looping and each time it is returning

  • 00:14:24 an item. Okay. You see the logic here, it continued to look but it is not breaking the

  • 00:14:30 loop.

  • 00:14:31 II will take a note in sandwich.

  • 00:14:39 Okay.

  • 00:14:46 Okay, and so let's continue with our story. So this is how it works and

  • 00:15:01 Okay, so we can also Define others rules here. I will show that as well. So we need to change

  • 00:15:12 our routing which we do in our lectures 8C HTML. I mean we've not seen as I'm in the

  • 00:15:23 controlled via not to be so,

  • 00:15:28 Let's get this route into our lecture, 9 controllers.

  • 00:15:34 Okay. So I will get I will copy and paste this, okay? This is nature eight controllers.

  • 00:15:43 Let's copy this and this makes your 9 controllers.

  • 00:15:51 So let's remove rooting for not a complexion because this is root rotting and here, okay?

  • 00:16:04 And this will take lecture 9 and to be able to use it, I need to add using lecture 9.

  • 00:16:13 Models.

  • 00:16:14 He is okay and it's going to return late June 9 leave which we put on the lecture, nine

  • 00:16:23 folders.

  • 00:16:24 Okay here as well and here as well. Okay, everything looks correct. And people always,

  • 00:16:33 I will also update this update, lecture nine model, this is the action name. And in our

  • 00:16:40 view let's copy and paste as well.

  • 00:16:43 Like this and this will election nine main page lecture, nine model action name control

  • 00:16:53 the name

  • 00:16:54 Okay, and let's modify our model.

  • 00:17:04 Let's say what is it is here, validation? So if it is below 19, people say, are you

  • 00:17:17 sure that old if you are below 12? Okay, so let's change this to

  • 00:17:27 1950. And and first on, let's make it like this.

  • 00:17:38 Let's round up the kitchen and test. Okay.

  • 00:17:42 And if there is any others.

  • 00:17:46 Are there any other thing that we need to do? Probably?

  • 00:17:56 No. That's okay. Let's run and see if it is working or not.

  • 00:18:08 Okay, lecture nine main page everything working. So, let's first try 1950. Okay, I'm going

  • 00:18:30 to change the Year, too.

  • 00:18:34 1950s.

  • 00:18:35 How can I do this fast?

  • 00:18:41 It problematic 19. Yeah, that's up from here. Okay, so now we are at 1948 and it should

  • 00:18:54 not get save it.

  • 00:18:56 Submit give you have an Android. Yeah we we are still passing lecture eight as a model

  • 00:19:06 in some bars.

  • 00:19:07 Probably somebody wants. Yes.

  • 00:19:11 Yeah, he is refresh Model, 8 is our problem. We need to change this refresh model 929 because

  • 00:19:27 we have generated a new model. Okay? Okay. Make sure 999. Yeah, we don't need to change

  • 00:19:39 anything else.

  • 00:19:42 And here you are officially to this one.

  • 00:19:48 Okay. And let's first start, I will also load

  • 00:19:56 repeating of email.

  • 00:19:57 But we don't need to add each time.

  • 00:20:01 Okay.

  • 00:20:02 Oh naughty, I can send it.

  • 00:20:10 Yeah, I can send it to you. I need to send it in, refresh. I think.

  • 00:20:27 In here. Yeah we are modeled email and repeat, okay?

  • 00:20:46 Let's first update, the valid date currently we are only having a custom validation here

  • 00:20:56 like this for our birthday. So

  • 00:21:05 Let's see. Okay, we still have okay, we are still calling somewhere late too, late model.

  • 00:21:11 Make sure 9 controller. Okay, my mother. So this is returning back to nine model and here

  • 00:21:21 we have the section 9 model

  • 00:21:24 Or we have update users.

  • 00:21:32 okay, maybe this is the cause

  • 00:21:37 No, this is not the reason.

  • 00:21:43 Oh, maybe in the VF.

  • 00:21:48 Okay, yeah, we have to use lecture 9 as a mother.

  • 00:21:57 Okay.

  • 00:21:59 Let's let's start and see.

  • 00:22:14 Okay, and let's enter the valley taste so this should be what it? What you updated.

  • 00:22:24 Let's refresh.

  • 00:22:26 Uh yeah save it now let's enter invalid State like 1945 submit. Okay so you see this is

  • 00:22:37 our custom message. It says surely you are not that old. Okay, so this is another message

  • 00:22:45 that we are returning in our model. The values you have answers are invited for, please fix

  • 00:22:51 errors and try again. So let's try another invalidate.

  • 00:22:57 As 2012.

  • 00:23:00 Okay. So it says sorry you are too young for this website and let's enter a value of this

  • 00:23:09 ambush

  • 00:23:10 Okay, so it returns both of the messages as you can see.

  • 00:23:17 You see, not only a single message, but both of the messages. Because now that we are doing

  • 00:23:29 yields return, it is checking every case, not only a single case. And so it is getting

  • 00:23:38 both of them. And let's refresh and try again to see

  • 00:23:47 Let's change, moans to December.

  • 00:23:50 Submit. So you see, there are two messages here. Actually, there is also two messages

  • 00:23:55 but it's not visual. I'm on you.

  • 00:23:57 Let's check from Court. Yes, you see. There are

  • 00:24:03 No, it only returns bond for here, but on here, we see both of the message. So also

  • 00:24:12 riding the summary of the error. Someone is good because this only displays single order.

  • 00:24:19 But here we get both of the errors, displayed as a summary. And we can also add other custom

  • 00:24:29 verifications such, as we can force, users email to start.

  • 00:24:36 Eight letters. Hey, let's try that.

  • 00:24:41 we'll just, let's type it from beginning if they start as our email starts with

  • 00:24:55 And I will add something like this email to. Yeah, that's make it like this, a okay, if

  • 00:25:07 it is not starting with letter, A yields return need validation results.

  • 00:25:16 Sorry your email has to start with letter, h k and then we will return if Earl Ray and

  • 00:25:30 inside this array.

  • 00:25:32 Okay, we will type or email name parameter name. Okay. Let's let's start our application

  • 00:25:45 and see if it is working as intended or not.

  • 00:25:55 All right.

  • 00:26:03 Okay, so let me update email with ad.

  • 00:26:12 Okay, let's try. Wait GA. Okay.

  • 00:26:19 Okay, now it returns are error. Surya limit has started today, I will also and Anton and

  • 00:26:26 invalidate, so it should also show that as long as well.

  • 00:26:34 Okay, here we can see all of the errors and it is working perfectly fine as we wanted.

  • 00:26:44 So, everything is working.

  • 00:26:47 Yeah, that's read somebody.

  • 00:26:54 Summary.

  • 00:26:56 Implementing your own custom, validation logic in your asp.net MVC, project is very easy.

  • 00:27:03 There are two methods one that ties directly to a specific property and one that ties directly

  • 00:27:08 to the model, allowing you to validate multiple properties at the same time, actually, I will

  • 00:27:14 prefer this method because I think this is better. However, the other one can also be

  • 00:27:22 used and here

  • 00:27:25 So, with this approach, actually, you can validate multiple different models as well.

  • 00:27:36 So this approach is specific to this model.

  • 00:27:41 and because it is inside this however, the other method which is this one,

  • 00:27:52 Can be used for any model because you just simply Define here so I can just use this.

  • 00:28:02 Okay, buddy. This

  • 00:28:06 Here as well. You see. It is what it it is perfectly valid.

  • 00:28:12 Okay. Alright, so let's take note of so far what we have seen.

  • 00:28:35 Let you know in context.

  • 00:28:47 How to change names, face of the project.

  • 00:28:54 Properly.

  • 00:28:57 How to implement and model specific custom.

  • 00:29:03 Pollination Gates.

  • 00:29:05 How do we do it? We do it bit, I waited eight, I Bali.

  • 00:29:15 Datable. Yeah, I wanted a table object and

  • 00:29:21 what it is.

  • 00:29:23 Okay. Oh it's not on low. Think YouTube comment, what addition context?

  • 00:29:38 And yields return.

  • 00:29:42 What is yields return and how to use it, okay?

  • 00:29:57 alright, so now we can continue

  • 00:30:04 to our tutorial.

  • 00:30:07 So, the series actually because you are doing a serious here, next? OKAY. Client-side model

  • 00:30:19 validation. This was server-side. Remember that you always have to validate server site

  • 00:30:28 You can, you can also validate client site that can be useful that can reduce your server.

  • 00:30:38 Workload, that can be helpful for your clients, save their data, their time. However, never

  • 00:30:47 trust clients, never trust client-side, validation because it can be

  • 00:30:59 By positive easily, manipulated changed modified for Jude. Whatever soaked server-side, validation

  • 00:31:09 is mandatory okay?

  • 00:31:15 Client-side model validation so far. All the model validation we have used in this tutorial

  • 00:31:21 has been done server side. This means that the model is passed back to the server where

  • 00:31:27 the validation is performed according to your data annotations, and then a result is returned

  • 00:31:31 to the user. This is both good and bad. It's good because you can't trust client-side,

  • 00:31:38 validation, the user can simply Bypass or change any type of validation performed on

  • 00:31:43 The Client.

  • 00:31:44 In other words, you always need to validate things on the server, to make. Absolutely

  • 00:31:48 sure that everything is as you expect them to be

  • 00:31:53 On the other hand the extra round trip to the server makes the validation a bit slower

  • 00:31:57 or a lot. If there's High latency between the server and the client and it also raises

  • 00:32:02 the resource requirements for your web application. If a lot of users are accessing it at the

  • 00:32:06 same time, a lot of extra validation calls to the server will be made. So to get the

  • 00:32:12 best from both worlds. You should first perform validation on the client. If any errors are

  • 00:32:17 found, you don't have to perform the server side validation.

  • 00:32:21 Instead you just immediately tell the user what's wrong? So they can fix it on the other

  • 00:32:26 hand. If no errors are found by the client side, validation, you make the call to the

  • 00:32:31 server and perform the validation there as well to make absolutely sure that everything

  • 00:32:35 is in order.

  • 00:32:37 Previously, this double validation approach would require you to write validation code,

  • 00:32:42 in both C, sharp, or whichever server-side language you used for the server and in JavaScript

  • 00:32:47 for the client, okay? So this is really important previously, these double validation approach

  • 00:32:54 busk cumbersome because you would have to spend extra time, duplicate time, and it wasn't

  • 00:33:04 appropriate as you can expect.

  • 00:33:07 However, now it is much easier with is that an MVC core, let's see. This was very cumbersome

  • 00:33:16 and could easily lead to in consistencies, fortunately, for us asp.net core tries to

  • 00:33:22 solve this problem for us. When you use data, annotations for your models, as described

  • 00:33:27 in previous articles and you use Tag helpers or HTML helpers to generate your forms, with

  • 00:33:32 the framework, will automatically generate validation data for the elements. Okay. So

  • 00:33:36 we are going

  • 00:33:37 To use talk helpers or HTML helpers. Okay. These are the key objects. And with this,

  • 00:33:46 we will be able to

  • 00:33:50 This data can be used by a JavaScript library for client-side, validation created by Microsoft

  • 00:33:55 on top of an existing JavaScript, validation library for jQuery. So Microsoft Java second

  • 00:34:03 Library will handle all the necessary work for us and we won't be have to write anything.

  • 00:34:10 This might sound complicated, but that's because good in robust validation. Is a complicated

  • 00:34:15 task.

  • 00:34:16 therefore, I'm going to do a complete walkthrough of how you can achieve both server-side and

  • 00:34:21 client-side, validation in your asp.net MVC application, using the same model,

  • 00:34:27 The model.

  • 00:34:29 In our example, we'll create a form for creating a new user profile for our imaginary website.

  • 00:34:35 The first thing we'll Define as our model called Web user which is very simple, it uses

  • 00:34:41 data annotations for all relevant properties to indicate how the model should be validated.

  • 00:34:47 It looks like this, okay, we already have such model here, you see? We have required,

  • 00:34:55 we have string length, Rick a display, eh,

  • 00:34:59 Us.

  • 00:35:00 Let's continue with controllers, the controller.

  • 00:35:05 We have a controller called users controllers, so far. It only has two methods and they are

  • 00:35:10 both called create. There's one for the get version, which will simply serve the view.

  • 00:35:15 And then there's the post version, which is called, when the form is submitted to create

  • 00:35:19 the new user, it will check if the model is valid, which is done automatically. Thanks

  • 00:35:24 to the data annotations and then return the view, here's how it looks.

  • 00:35:29 Okay, we already have that here in our control of the see that okay in index which hosts

  • 00:35:39 methods. You see, we are checking if model is valid, if it is not, we are returning a

  • 00:35:50 message, we are sending a message in our model and we are returning it to the weave and be

  • 00:35:57 displayed.

  • 00:36:00 The most interesting part is the check of the model state DOT is valid property. This

  • 00:36:05 is automatically filled by the framework after the model has been validated according to

  • 00:36:09 your data annotations,

  • 00:36:12 The View.

  • 00:36:14 The view is a pretty basic form to support the properties of the web user class will

  • 00:36:19 use Tag helpers. They are described in detail elsewhere in this tutorial to generate labels

  • 00:36:25 input fields and validation output. Notice how little markup we need to accomplish all

  • 00:36:29 of this?

  • 00:36:30 Okay, so we need to use talk helpers.

  • 00:36:38 And let's see where they are. Define it.

  • 00:36:47 Okay, there's nothing.

  • 00:36:55 Okay, so let's see. Where are the talk? How does that is? Is before Labor?

  • 00:37:08 Okay, so there is spawned is p validation forth.

  • 00:37:38 okay, so I think we can add

  • 00:38:07 this.

  • 00:38:08 Talk help us with our current form. So we have validation message for, and now we need

  • 00:38:21 to head at

  • 00:38:22 Why did they shoot for? And how we going to do that?

  • 00:38:39 Maybe we will, he need to define it.

  • 00:38:41 There is labels for yes, that is labeled. And there is input, which is textbox false.

  • 00:38:52 Maybe this can work. Let's try it. So I will put this and

  • 00:38:56 Yes, and let's see if it will work or not. So this will be the this

  • 00:39:07 And I will add is necessary client-side scripts to The Head section of my page.

  • 00:39:17 Okay, so before the H1, okay, let's add script.

  • 00:39:29 Make sure these are all going to these already have script talk. Okay, this probably got

  • 00:39:35 updated but it should work.

  • 00:39:44 So this is field validation for IR user ID, should generate automatic code for client-side,

  • 00:39:58 validation.

  • 00:40:03 Okay, let's check the source code.

  • 00:40:11 Okay, we have and he has a validation Force.

  • 00:40:20 so,

  • 00:40:21 There is State about addition. That's answer something like this.

  • 00:40:28 Okay, so you see it is automatically generated about, this is not the case may be.

  • 00:40:38 And now it should prevent. Okay. So it says that the antibody was not a valid integer,

  • 00:40:42 I am not able to post this anymore, so you see it is not posting back to the server's.

  • 00:40:49 So

  • 00:40:50 Let's make it something like this. And first name is supposed to be a minimum 3. So when

  • 00:41:08 I submit like this,

  • 00:41:10 We will submit happens. Let's check the network.

  • 00:41:18 I think it is even working right now. Without I am need to add anything.

  • 00:41:24 Which I will test something. Well, I've already moved this and it'll move these secrets.

  • 00:41:34 And run the page, okay, because I have an idea, I think when we add validation message

  • 00:41:44 for it is automatically generating those but to be sure. Let's see.

  • 00:41:51 You could also generate your code like shown here. It is totally fine.

  • 00:42:02 But I see it actually this is even better, I think.

  • 00:42:09 But let's first very Pines. So this is our source code. Let's put it here.

  • 00:42:20 And let's make it test.

  • 00:42:23 Okay, you see, there is nothing appearing here until I post back, so Prospect happens

  • 00:42:30 and it says invalid. Now let's add

  • 00:43:06 Our secrets here.

  • 00:43:09 When I click somewhere else.

  • 00:43:13 Then I click somewhere else. It says the errors.

  • 00:43:17 Okay, I will try 12. Click somewhere else. Oh, I'll custom, validation is not coming

  • 00:43:27 here. So it is fin server side. Does this count? Okay, so let's try. Yeah. Customization

  • 00:43:36 not appearing here, but the other ones are appearing here. Let's check the source code.

  • 00:43:41 Here. And let's compare the sore spot.

  • 00:43:47 See if there is okay.

  • 00:43:52 Compare text actually differences.

  • 00:43:56 Kdf. Checker is a good one.

  • 00:44:02 okay, so let's put

  • 00:44:05 Left side. The NIV and the right side their reviews.

  • 00:44:13 Okay, so in the source code, there is no difference.

  • 00:44:20 So it is not available in the source code, but probably it is loaded as JavaScript and

  • 00:44:28 bind to the elements.

  • 00:44:31 Okay, so in the source code, no difference. But it shows here.

  • 00:44:38 And it will not allow post fake. So you see Prospect is not happening. However, if I remove

  • 00:44:46 these secrets,

  • 00:44:48 Like this, the prospect will happen and it will not show me the address immediately.

  • 00:45:03 Let's retry.

  • 00:45:12 Okay, so I type like this, you see know I'd rush and I can submit so I can also check

  • 00:45:23 the network submit and it is submitting, however, when we add the secrets, it is automatically.

  • 00:45:32 Generating necessarily JavaScript. Let's see, where can we see this automatically generated

  • 00:45:40 JavaScript since it is Javascript? It is not visible on the source code, but maybe we can

  • 00:45:51 see it on Chrome.

  • 00:45:55 Okay.

  • 00:45:57 I don't know if this is possible.

  • 00:46:12 Even listeners in the elements. Let's try, for example, let's check this inspect in the

  • 00:46:21 source code. There is no difference, but when we checked event listeners, and where might

  • 00:46:28 it be,

  • 00:46:30 Okay, Chrome that is event listeners at the elements panel. So

  • 00:46:42 Okay, here elements panel. We are there and then events listeners. And here's

  • 00:46:51 Jada is Kaden.

  • 00:46:54 And there is Keo. You see in the K up, there is equal to validate and it says, you sculpture

  • 00:47:03 positive handlers and the Handler has

  • 00:47:10 Some functions another functions.

  • 00:47:14 Okay, so this is the key up validate. So it is coming from what it is and this is coming

  • 00:47:28 to my car. Spastic. I

  • 00:47:30 Let's see, the submit button if there is any okay. So in the class, first case in the submit

  • 00:47:37 button, that is validate. And if I remove this ticket, I am pretty sure that won't be

  • 00:47:45 them.

  • 00:47:46 So, this is how they are getting executed, how they are getting binded.

  • 00:48:04 Okay, let's check again.

  • 00:48:20 In Spite and you want listeners. Okay. You see that is zero event listeners right now.

  • 00:48:28 Assign it to this or

  • 00:48:33 Submit button and then we add that they harsh.

  • 00:48:47 And, let's inspect.

  • 00:48:58 Do you have the sense? Okay you see another event listeners are bombs from Casper sky.

  • 00:49:18 Submit jQuery mean reset, Upstate. Invalid, form, you see Jake revalidate on top three

  • 00:49:29 Civ?

  • 00:49:30 Okay, and then click submit, it will be submitting and validating as expecting, all right? Everything

  • 00:49:39 is working as we wanted we could also use like this. It is completely same. That's also

  • 00:49:47 generated as well. Yes, that's a good idea to generate that model as well. I mean in

  • 00:49:52 that style let's say as well.

  • 00:49:59 So here's the start is porn method equal to post? Okay, then we set our is without the

  • 00:50:10 controller, which defined on the SP controllers.

  • 00:50:15 I would expect it to auto complete, but it is not interesting. OKC controller will be

  • 00:50:31 lecture 9 controllers.

  • 00:50:37 Oh, I think it is not even riding to controller part.

  • 00:50:44 Okay, so it will automatically check with lecture 9 controllers, then s reaction. So

  • 00:50:52 the action will be equal to in our model, our action name

  • 00:51:01 In our control. Actually, let's check it out. Is update this one.

  • 00:51:08 So this, okay, so this is the generic form and then there is deep. So it starts in the

  • 00:51:20 Divi starting to be

  • 00:51:23 You can do all the styling you want, and then we will generate input. So the first input

  • 00:51:32 will be labeled.

  • 00:51:35 okay, so the labels will be equal to

  • 00:51:37 User ID, you see label. Then it goes to the next line, if you want, or you can keep them

  • 00:51:47 in the same line, I will do that. So there will be the input like this and input will

  • 00:51:54 be like this. Then we will add validation message so input field by default go to the

  • 00:52:03 next line. Probably,

  • 00:52:07 Is it on? Okay. And then we will make the other ones.

  • 00:52:13 So, user ID first name.

  • 00:52:16 Okay, first name first name first name, and in the first name, we have a placeholder.

  • 00:52:28 So that's 2/3 as well.

  • 00:52:32 And placeholder will be equal to please. And

  • 00:52:36 The first name.

  • 00:52:39 We can also put placeholder for this placeholder equal to please and using ID. By the way,

  • 00:52:48 this place holders will be visible. Only when there is no text entry to them and then we

  • 00:52:55 will have a last name.

  • 00:52:59 Last time, let's remove this.

  • 00:53:01 Okay, we get the idea last name and we have email.

  • 00:53:18 We have emailed me have email repeats. So for email repeated, how do we check

  • 00:53:28 Time, whether they are meeting or not.

  • 00:53:40 I think it is check it at the shorter side so yeah, it will go automatically handle it

  • 00:53:47 properly so let's add email repeated as well.

  • 00:53:53 I'm done.

  • 00:53:59 We have.

  • 00:54:01 What can we have birthday?

  • 00:54:07 And the birthday will be.

  • 00:54:13 Like this. However, we will set the body of birthday, so this will be, this will have

  • 00:54:24 type as date and volume will be equal to same as here.

  • 00:54:37 So for Value setting, we need to use modem dots, ET birthday.

  • 00:54:46 K2 string.

  • 00:54:49 With custom.

  • 00:54:52 Okay, string here.

  • 00:54:58 I think you should vote. We're not sure, but let's try.

  • 00:55:12 and,

  • 00:55:13 Okay, stink Walmart.

  • 00:55:18 Shooting.

  • 00:55:20 Like this.

  • 00:55:23 And then we have submit button and validation summary. So that's the submit button.

  • 00:55:39 Let's use this and let's add validation stomach. Okay? So for to be able to use validation,

  • 00:55:50 so many

  • 00:55:52 Okay. What is the validation somebody HTML form?

  • 00:56:02 Maybe spawn validation summary here. Let's try that.

  • 00:56:10 Spiritual elevation.

  • 00:56:13 if it's something like this,

  • 00:56:44 okay.

  • 00:56:52 So this is the talk Halpert. It is like this. Yes.

  • 00:56:59 one dacian, for

  • 00:57:00 Where is the validation summary?

  • 00:57:07 Oh, here.

  • 00:57:13 And Dave I see validation somebody for all okay? And then finally we can show our model

  • 00:57:22 post results here as well.

  • 00:57:25 Like it. So that's it on the application and see if it will work as expected or not.

  • 00:57:43 Okay, so you see, these placeholders are visible, but all values are not loaded because we didn't

  • 00:58:06 set them. However, date is lotus, perfectly fine as expected. So let's also raw loads

  • 00:58:13 our values and to be able to load them. You see, we need to load volume is equal to its

  • 00:58:22 model Dot.

  • 00:58:24 I are user ID like this.

  • 00:58:31 Volume 4 is our first name.

  • 00:58:37 Oh, this is last name, by the way.

  • 00:58:43 Okay, is our last name. Last name and email.

  • 00:58:54 And here's we need to set aside image, repeat it.

  • 00:59:01 Okay, let's try again.

  • 00:59:10 Okay.

  • 00:59:17 Okay, okay, everything is loaded. So when we take the forms, I wonder if there is any

  • 00:59:36 different. So there is formation methods. No validate, nobody date. It says and we have

  • 00:59:43 SP controlled election an inspection. Oh

  • 00:59:48 I wonder if this let's set these ads TV 425

  • 00:59:57 Okay, it's sad that you have an Android. So

  • 01:00:04 Appropriately. We have is to control the problem.

  • 01:00:09 Maybe we need to type full name.

  • 01:00:17 I feel so this is becomes Ruble crate.

  • 01:00:31 Action types of meat.

  • 01:00:35 They have action create. Yeah, say and the control, the name user controller.

  • 01:00:49 So I didn't try to fool neck, full cat.

  • 01:00:51 So why do we have a problem?

  • 01:01:07 so maybe it is posting Board of the

  • 01:01:26 Metals. But it shouldn't. Let's give them an ID. Perhaps.

  • 01:01:37 No, no not relate to that.

  • 01:01:50 Nature 9 controller and update next to nine, modern, which is here.

  • 01:01:57 so, I will just

  • 01:02:01 Okay, comment this out to see what happens in the code behind.

  • 01:02:15 And let's see.

  • 01:02:27 Let's see what happens.

  • 01:02:29 If it is posting correctly or not. So this is the controller name.

  • 01:02:37 And action name.

  • 01:02:42 Okay, so my model has arrived.

  • 01:02:48 Okay, it is not what it. Why?

  • 01:02:53 Okay, because the values are not Magic.

  • 01:03:02 Why is that, do you think?

  • 01:03:06 Okay. It is input. Like this is for first name, Place holders.

  • 01:03:24 Let's remove this and let's see how it is generating the source code so we can figure

  • 01:03:28 it out. What is the error?

  • 01:03:36 Okay, this model we also need had have set the model here.

  • 01:03:55 What's your for example? This has been beautifully. Like this is for is our first name and this

  • 01:04:05 has

  • 01:04:07 Input data while true data length.

  • 01:04:13 You see this has ID.

  • 01:04:17 So this is before is not

  • 01:04:19 working.

  • 01:04:25 Why? Because there is sp for class, SPF for and is provide education for

  • 01:04:41 Only okay.

  • 01:04:42 Table, SP for input a speed force.

  • 01:04:52 I won't ask for Action cards.

  • 01:05:05 The generated codes are not same.

  • 01:05:13 Why?

  • 01:05:20 Call.

  • 01:05:26 Okay, so this

  • 01:06:19 is html4 and this is sp forth.

  • 01:06:33 Label input on spontaneous validation Porsche.

  • 01:06:44 Okay, this is not working.

  • 01:06:57 OJ's enabling Tock helpers.

  • 01:07:04 I see so to be able to enable them, I need to add talk helper. Okay, so let's add this

  • 01:07:13 into our we've import file. So we will enable it in all our pages and where is our will

  • 01:07:21 import file? It is

  • 01:07:23 Well, we don't have it at the moment. Yeah, we didn't edit yet.

  • 01:07:36 So let's edit to talk like this. So we need to add stock help us and enable them, then

  • 01:07:44 they should work. Let's see.

  • 01:07:50 So this is how the ad-hoc helpers.

  • 01:07:55 Okay, in the tutorial.

  • 01:08:04 Yeah, it doesn't enable talk helpers.

  • 01:08:12 It should have ended it. Therefore it is not working. Okay. It says tuck has a citizen

  • 01:08:17 to whatever manually enable here and let's retry.

  • 01:08:22 Okay, now it is changed. You see? Now I can also use like this and

  • 01:08:30 Kate is watching this update at users.

  • 01:08:37 Is what is true? Nice, it is working and okay, okay, it's updated or some so let's make it

  • 01:08:48 like this D, B5 and jb5 nice. It is working let's try something else like this. Okay it

  • 01:08:57 says sorry your email has started a lady a

  • 01:09:03 Okay. And let's try something like this. Okay now, okay, client-side, validation also working

  • 01:09:13 and then we delete everything. It shows the

  • 01:09:19 Okay, placeholders. Alright, everything is working.

  • 01:09:26 Okay.

  • 01:09:29 It didn't post back yet because they made was not matching.

  • 01:09:33 Okay, nice, it is working. So if I change values here but I don't post vague.

  • 01:09:41 That, let's see what happens.

  • 01:09:46 Okay. It is still updating because we are not pushing this button.

  • 01:09:51 They are different forms. And so when I check the source code,

  • 01:09:56 okay, there is for method action here. Nobody date. And no validate. Yeah. It is working

  • 01:10:03 exactly as it should be.

  • 01:10:08 All right. Nice. So now we can move to our next article by the way, if you want to enable

  • 01:10:17 talk helpers in your all your pages, then you should have a VGA port. We have seen that

  • 01:10:24 previously. We've Imports.

  • 01:10:25 And then it would be enabled in all pages by default.

  • 01:10:35 Okay.

  • 01:10:38 All right.

  • 01:10:42 So let's read this part. When you reload the page, you will notice something. Cool. Validation

  • 01:10:50 is performed automatically. As soon as you start filling out the form, and if you click

  • 01:10:54 on the create button, the form is only submitted to the server. If the form appears to be valid,

  • 01:11:00 if it is, and then form is sent back to the server. It will be server-side validated according

  • 01:11:05 to the same rules, which you have only specified in one place. This is really the beauty of

  • 01:11:10 tying together the various components of the asp.net.

  • 01:11:13 Net core framework.

  • 01:11:16 Please notice.

  • 01:11:18 The first line is just the regular jQuery framework. If you already include this elsewhere,

  • 01:11:23 you don't need to include this line. All references, here are for a specific CDN content delivery

  • 01:11:30 Network. You can choose another CDN or download the files and serve them directly from your

  • 01:11:35 website. All references here are four specific versions jQuery version, 3.4 Point 1 and so

  • 01:11:42 on. You may want to check if newer versions are available.

  • 01:11:46 Summary validating user input has always been a pain, it requires a lot of effort and repetitive

  • 01:11:53 code, especially if you want both client-side and server-side validation. However, as shown

  • 01:11:59 in this article, the asp.net core framework makes this task a whole lot easier, faster

  • 01:12:04 and more robust. Yes, definitely true. So let's take our notes.

  • 01:12:10 Oh, to add client side, validation and prevent Miss input. Push back to the

  • 01:12:25 Service.

  • 01:12:27 so, to be able to,

  • 01:12:35 Violation automatic.

  • 01:12:42 Which gql validation. Yeah.

  • 01:12:55 Also, how to auto-generate.

  • 01:13:05 Okay.

  • 01:13:11 Using, is it help us?

  • 01:13:20 How to enable?

  • 01:13:24 Which is batak. I'll burst in.

  • 01:13:36 It's not much.

  • 01:13:44 Heels.

  • 01:13:47 And everything is working or soy, okay. So you see, after I have added this into my page

  • 01:13:58 then the color of these are changed. You see now it shows because of days without the code

  • 01:14:03 and receive. If features model exploration

  • 01:14:07 So now they are differently, formatted color it. So, you should also expect this to see,

  • 01:14:17 all right, let's move to the next.

  • 01:14:26 Okay.

  • 01:14:28 Okay, let's continue.

  • 01:14:34 Manual model, validation with data, annotations.

  • 01:14:38 When using model, validation with data annotations, the MVC framework will usually handle, most

  • 01:14:44 of the process of Performing the actual validation, and even presenting the results to the end

  • 01:14:48 user in the way you want it. However, as we already talked about a cool thing about the

  • 01:14:54 MVC pattern is the separation of concerns. This means that while you may build a nice

  • 01:14:59 collection of models to represent all the data of your web application, these models

  • 01:15:04 can also be used outside of the MVC framework.

  • 01:15:06 EG in a desktop application. How is that possible? Because models are just regular dotnet classes.

  • 01:15:16 But why would you ever need to use your models outside the MVC framework? Well, there's a

  • 01:15:22 good chance that you won't. It really depends on what you're working on. But as an example

  • 01:15:27 consider a larger project where you need to automate some actions and run them directly

  • 01:15:31 from the command line instead of through the web server.

  • 01:15:35 This will allow you to schedule these actions Through the Windows task scheduler or as a

  • 01:15:39 Cron job on asterisk. Nick's without having to worry about timeouts and other web related

  • 01:15:44 limitations.

  • 01:15:46 In a case like that, you can easily reuse your models in another type of application,

  • 01:15:51 eg a console app, just add a reference to the project, where you keep your models, which

  • 01:15:55 can be a separate project or part of your web project and start using the model classes,

  • 01:16:00 just like any other class. This is awesome. But what about validation since the MVC framework,

  • 01:16:07 handles, a lot of the magic involved in performing the actual validation. It's not quite clear

  • 01:16:12 how to take advantage of the validation rules. You have added to your models using

  • 01:16:16 Annotations.

  • 01:16:18 Performing a manual model validation.

  • 01:16:22 Let me demonstrate how to accomplish this, we have the web user model, which we used

  • 01:16:27 in previous examples in this chapter. Okay, so we already have something like this in

  • 01:16:33 our model and let's review it here our model which uses data annotations

  • 01:16:41 Okay.

  • 01:16:46 As you can see, its properties have been nicely decorated with validation, annotations specifying,

  • 01:16:51 the various requirements. These rules are what we are looking to take advantage of outside

  • 01:16:56 of the MVC application. Therefore, I have created within the same solution a console

  • 01:17:01 app project to demonstrate this. Obviously this is just to demonstrate an alternative

  • 01:17:07 use case for these mechanisms. You may use the same approach within your MVC applications

  • 01:17:11 in case you want more control over the validation process for some reason.

  • 01:17:16 Here's our console app.

  • 01:17:18 Okay. In our console application, we have the same thing and that is what it is, er,

  • 01:17:26 trait validate, our object, user context results through is valid.

  • 01:17:34 Okay, so

  • 01:17:35 how we gonna do it in reality?

  • 01:17:48 so you see, currently

  • 01:17:51 My model is using namespace, lecture nine models, okay? And when I open my folder,

  • 01:17:59 In my binary, debug folders.

  • 01:18:07 I see lecture 9, dll five. Okay, so let's compose a console application and add this

  • 01:18:21 dll file as a reference to our project and see what happens. Okay. Create a new project

  • 01:18:29 and since my model is public, I should be able to access all elements inside it all

  • 01:18:36 prepared.

  • 01:18:37 Are these attributes fields and whatever there are.

  • 01:18:42 I will also compose a console application.

  • 01:18:46 To demonstrate how we can use, how we can do a manual validation.

  • 01:18:55 And reuse our mode, okay? So I will save this as

  • 01:19:07 to hear lectures.

  • 01:19:09 Setting for those lectures 9V use mod.

  • 01:19:14 She always says, Jeb CMD.

  • 01:19:20 So it won't be happy. It won't have a very long name, okay? Use the targets will be dotnet.

  • 01:19:28 Five current framework dotnet core actually

  • 01:19:32 And this is just renamed.

  • 01:19:37 Okay.

  • 01:19:39 Okay just getting loaded. Okay so this is the simple program and now I am going to add

  • 01:19:53 a reference

  • 01:19:54 to my project.

  • 01:19:59 Okay, it's project represents project opens compliment, service reference.

  • 01:20:07 It's new items.

  • 01:20:09 Online. How do I edit?

  • 01:20:13 Can I see it?

  • 01:20:17 Look at the Bunny at project open sites. Let's see if it project reference box and I'm not

  • 01:20:33 sure.

  • 01:20:34 Rose. I will use browse here and let's copy and paste. These two text top and we can easily

  • 01:20:44 find it in the desktop. Make sure 90 LL. Okay, okay. And see what happens.

  • 01:20:52 Okay. Is this edit? And now I'm going to add using lectures 9. So this is my name, space

  • 01:21:05 of other project and now I should be able to generate models from my lecture 9. So lecture,

  • 01:21:15 9 dots

  • 01:21:17 Models that lecture 9. Okay? And my custom model.

  • 01:21:26 So, I am.

  • 01:21:29 Unless a lectures nine model, I am generating an instance of my model. And in here.

  • 01:21:42 I can I want to use validate. Okay, I want to use validation. So how am I going to do

  • 01:21:56 that? I will set values. Okay? Such as birthday will be equal to NIV daytime. And for example

  • 01:22:06 1950 or 1959 three tree. Yeah. Like this and

  • 01:22:15 lecture 9 model dots user ID 2 which is 9 modem dots email. Okay, let's make it like

  • 01:22:30 this.

  • 01:22:31 okay and lecture, 9 mother doubts, you may repeat its test so it should throw at her

  • 01:22:42 nose Nation, nine model dots, first name, a

  • 01:22:47 Make sure 9 model dots.

  • 01:22:52 Last name.

  • 01:22:53 Something like this.

  • 01:22:57 Selection, I model dots, post message. Now we will set it later and what other property

  • 01:23:08 we have here.

  • 01:23:11 Okay, nothing now let's see the tutorial. So we are going to use this in our CMD project

  • 01:23:24 one edition context.

  • 01:23:25 so this validation context is coming from using system component model data, annotations

  • 01:23:34 and it will take my

  • 01:23:40 object instance, you see there is object instance, so it can be any object and others with null

  • 01:23:47 then.

  • 01:23:53 It will return validation results. Okay? So for validation result, I need to add system

  • 01:24:01 collections generate

  • 01:24:04 Now we have results list then we will use is valid here. Well, it is your driver, a

  • 01:24:12 date object. So this validated is coming from system, component, model user, annotations

  • 01:24:19 and give us what we will validate or model, it will take context. So the context is validation

  • 01:24:26 context of lecture, 9 model and results will be this. So it is like

  • 01:24:34 Strike parts and then there is true. So it will return true and is what it is true or

  • 01:24:44 false. And if it is not valid we will parse the errors to do screen like this week. If

  • 01:24:54 it is not valid user is not worried and validation results and you see error message and let's

  • 01:25:02 run the application.

  • 01:25:04 Creation.

  • 01:25:05 It's a pretty simple code and you should be able to use our model in a completely different

  • 01:25:13 application. Okay, so it says user is not valid errors to create first name, must be

  • 01:25:18 a sitting with minimum length of 3 and maximum length of 25 email and S are arranged repeated

  • 01:25:24 to not much down. Actually, we also have more errors

  • 01:25:31 Such as we have error of daytime, right? So we use custom validation.

  • 01:25:46 Yes.

  • 01:25:50 However, it didn't work.

  • 01:25:58 In our project.

  • 01:26:02 So this date was a was an invalid state, but it didn't work. Last name was also invalid.

  • 01:26:08 Oh, no. Last name. We don't have a

  • 01:26:14 Yeah, we don't have any room for last name and we have email first errors. Okay, so let's

  • 01:26:24 change email. Oh email also didn't work. So all custom validations didn't work in here

  • 01:26:36 because we are not using validate.

  • 01:26:39 We are using validation context.

  • 01:26:45 Once you travel it is a thousand years.

  • 01:26:58 I wanted a table.

  • 01:27:23 Would you someone already asked a question, probably?

  • 01:27:33 Okay, so it is doing a shortcut. Therefore let's change it to something like this.

  • 01:27:43 And let's answer them like this and see what happens.

  • 01:27:49 no, you know, I think you know that's test

  • 01:28:00 This.

  • 01:28:02 So you should pass first name.

  • 01:28:05 Or now our customizations are working. Yes. So first it is checking this first validations

  • 01:28:17 done. If All Passes it is checking this one. So both of them is working great. It is great.

  • 01:28:25 So if we use like this instead of try

  • 01:28:29 Maybe we can use another think that is.

  • 01:28:42 Okay yeah no it is working fine. Yes. Now which is doing further check and it is showing

  • 01:28:48 the other errors as well.

  • 01:28:54 So there is also validate all properties flag. I think I will use that if there is such thing.

  • 01:29:00 Yeah, validate. All properties.

  • 01:29:03 Now, we should show everything so I will make them something like this.

  • 01:29:10 We says, you have already given that what other options that are.

  • 01:29:31 Anyway, it is working as expected and so it is doing a shortcut.

  • 01:29:40 There's also get object validation errors, what is that? Let's see.

  • 01:29:48 Validated.

  • 01:29:49 That's gets no. We don't have that.

  • 01:29:56 Maybe it requires any some other reference.

  • 01:30:09 There's also going to be tomorrow.

  • 01:30:39 Okay, so there is another stink.

  • 01:30:48 Okay, we don't need that. We are, we are getting everything we need. So if there is an errors,

  • 01:30:57 it will first display them. And if there is no errors, it will go further and also check

  • 01:31:05 this validate. So first it is checking the regular validations here and if they passes

  • 01:31:10 it is checking this validation. So it is working as expected, okay. And we have seen that so

  • 01:31:20 that's all

  • 01:31:22 Not that.

  • 01:31:23 How to?

  • 01:31:25 How to use your MVC model?

  • 01:31:32 Directly in an another application with tll referencing. Okay? How to manually validate

  • 01:31:47 your MVC model through? Okay, validator. Try to validate object

  • 01:31:56 Let's take a note here.

  • 01:32:00 Is first.

  • 01:32:02 Chase regulars requirements. And if All Passes it done checks,

  • 01:32:13 Let's say I wanted a table object.

  • 01:32:22 Okay, if any of the regular checks fails, its shortcut knots check costume.

  • 01:32:37 So we also added and notes.

  • 01:32:42 And this wasn't manual.

  • 01:32:44 I think this was a good example.

  • 01:32:54 alright, so let's read from here, summary

  • 01:33:04 using just a few lines of code. We can take advantage of the validation rules specified

  • 01:33:08 on your models. Using data annotations and validate models wherever we want to and do

  • 01:33:12 whatever we want to with the results. This can come in handy in several situations, EG

  • 01:33:18 when you want to reuse the validation rules outside of the MVC framework, or when you

  • 01:33:23 want full control of the validation process, okay? So the next will be talk helpers, okay,

  • 01:33:30 I think I will stop.

  • 01:33:32 Here for this week.

  • 01:33:34 You have ash left.

  • 01:33:37 And let's upload our project.

  • 01:33:46 If you have any questions regarding to your project regarding to your our lectures? Please

  • 01:33:54 ask them through this course. And let's see where we are. We are at the TOC helpers. Yeah.

  • 01:34:07 And then, what is the next?

  • 01:34:09 Then there is Core Concepts.

  • 01:34:13 I still context caging and working with databases. Okay, we have already seen

  • 01:34:21 Databases. So I think we can get these parts faster. And if we can't finish our lectures

  • 01:34:34 in 14 weeks, I will also record extra video so don't worry about that and try to understand

  • 01:34:42 everything we are doing because it's without net MVC. Is I think the

  • 01:34:54 Best web development framework and it will be also used in Futures more than. Now it

  • 01:35:05 also teaches you the fundamentals of how to do programming, how to do, how, to program

  • 01:35:13 in C sharp, and learn more about more complex things about C sharp and such or in.net.

  • 01:35:21 Net, net core. So it is useful from multiple.

  • 01:35:26 Points. Multiple waves angles, perhaps.

  • 01:35:34 Okay.

  • 01:35:36 I can, let's add them.

  • 01:35:44 I think I would also add.

  • 01:35:48 Backup of the project into the folder as well so you can restore database if you want.

  • 01:35:56 Do we have that in right now? Okay. We have PMS. This is I think correct one. Yeah. We

  • 01:36:08 should have our other database. PMS is not the correct one.

  • 01:36:12 So the correct database is missing. Yeah, I need to add this. So let me delete all the

  • 01:36:20 Run.

  • 01:36:22 In DV or we don't even have it. So let's task backup.

  • 01:36:31 Oh yeah, it wasn't incorrectly named. So I will name it as

  • 01:36:40 ABI me. See, let me see that ehe.

  • 01:36:47 Okay.

  • 01:36:49 and then,

  • 01:36:50 Let's copy and paste it here. Yes, let's check the our record users

  • 01:36:59 Okay, they're there. They are working. Nice. So let's

  • 01:37:12 TV changes.

  • 01:37:16 Okay.

  • 01:37:18 Thanks teacher 9 source.

  • 01:37:26 Old and Vivi backup file.

  • 01:37:37 Okay.

  • 01:37:39 Okay, great.

  • 01:37:43 Okay, end of lecture 9. Hopefully see you next week. Please try to be careful of coronavirus

  • 01:37:55 covid-19. It is very serious, be safe. See

Clone this wiki locally