Custom validation attribute in asp net core

  Рет қаралды 67,590

kudvenkat

kudvenkat

Күн бұрын

Пікірлер: 63
@MmMm-tg5mq
@MmMm-tg5mq 5 жыл бұрын
a whole academy in one person, thank you so much CREATIVE GUY
@rmm1332
@rmm1332 3 жыл бұрын
You are a gifted teacher! I understand everything you say, you are very articulate on explaining them without making me feel like I'm stupid. I have this issue to create custom validation on one special field called cost centre based on SAP validation. I'm going to try this next Monday. Wished me luck!
@menakasattmann5335
@menakasattmann5335 5 жыл бұрын
You are the great lecturer I ever met. all the best
@maikeru_pk
@maikeru_pk 5 жыл бұрын
I affraid that anyone type only "test@" and click register your code crash :) But it's a detail. Great video, like always!
@admiremhlaba1192
@admiremhlaba1192 5 жыл бұрын
Thank you for the pointer, I missed this one too, yes index 1 would be null and an exception would be raised....but I noticed that I have EmailAddress DataAttribute on Email Property so test@ would be an invalid email address that the client-side validation would pickup and block posting it.
@maikeru_pk
@maikeru_pk 5 жыл бұрын
@@admiremhlaba1192 Kud Venkat is a woman? 🤔
@admiremhlaba1192
@admiremhlaba1192 5 жыл бұрын
@@maikeru_pk I don't believe you but thanks for the heads up
@maikeru_pk
@maikeru_pk 5 жыл бұрын
@@admiremhlaba1192 No no it's just a joke :) Sorry, God bless you Admire :)
@admiremhlaba1192
@admiremhlaba1192 5 жыл бұрын
@@maikeru_pk Be Blessed More My Human Friend - I don't know your gender so I'm using Generics
@vssrikanthgarnepudi5952
@vssrikanthgarnepudi5952 5 жыл бұрын
Hi Venkat how to make a custom validation attribute to work on client side.
@levitkanner6055
@levitkanner6055 3 жыл бұрын
You're amazing!
@shahidwani6445
@shahidwani6445 5 жыл бұрын
Sir, how can we have client side validations for custom attr
@Abhimanyukumar-vb8bz
@Abhimanyukumar-vb8bz 4 жыл бұрын
Do you got the answer??
@gabrielbarreto5377
@gabrielbarreto5377 4 жыл бұрын
This worked to me. //Validation attribute public class ValidEmailDomainAttribute :ValidationAttribute, IClientModelValidator { public string AllowedDomain { get; } public ValidEmailDomainAttribute(string AllowedDomain) { this.AllowedDomain = AllowedDomain; } public override bool IsValid(object value) { return (value as string).EndsWith(AllowedDomain); } public void AddValidation(ClientModelValidationContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } context.Attributes.Add("data-val", "true"); context.Attributes.Add("data-val-ValidEmailDomain","Error: "+ ErrorMessage); context.Attributes.Add("data-val-AllowedDomain", AllowedDomain); } } //javascript in the view (function ($) { $.validator.addMethod('data-val-ValidEmailDomain', function (value, element, params) { debugger; value.slice() var domain = $(element).attr('data-val-AllowedDomain'); if (domain) { return value.slice(-1 * domain.length) === domain; } return false; }, function (params, element) { debugger; var msgCompare = $(element).attr('data-val-ValidEmailDomain'); if (!msgCompare) { msgCompare = 'Domain of email is not allowed.'; } return msgCompare; }); $.validator.unobtrusive.adapters.addBool('data-val-ValidEmailDomain'); })(jQuery); There is another example in the below link[1], I used it as reference ( but the article is in portuguese). [1] medium.com/@fulviocanducci/aspnet-core-valida%C3%A7%C3%A3o-customizadas-server-side-e-client-side-55a7972757d4
@leozhang4574
@leozhang4574 4 жыл бұрын
Good question. Maybe we have to write the validation logic as a "Remote" action instead of a custom validation attribute?
@debarghyachakraborty
@debarghyachakraborty 4 жыл бұрын
@@gabrielbarreto5377 Thanks, worked.
@mati2901
@mati2901 11 ай бұрын
why do you use the ErrorMessage prop from the base class, isn't there a way to set it through the base() ctor?
@vinothdharmaraj7510
@vinothdharmaraj7510 5 жыл бұрын
Super venkat and one more thing i don't know why all people skip custom client side validation for Core version. Please consider
@ionutenache6473
@ionutenache6473 3 жыл бұрын
How can you trigger the custom validation attribute on lost focus? I want my custom validation to work exactly like the EmailAddress or Required attribute
@sakthir3892
@sakthir3892 5 жыл бұрын
Nice explanation sir
@muttBunch
@muttBunch Жыл бұрын
Hey, thank you once again for your detailed explanation. I guess you cannot do something similar to this if you are using a shared class library, can you? My models are stored in the class library adjacent to my web project. I have a reference from my web project to the class library but it will not allow me to use the custom attribute from the class library back to the web project. It's working to an extent as long as the custom validator is coming from the class library but since the custom validator is not in the web project, it's not showing the validation in red when I hit submit. But it's partially working as long as I have a "." in my input field which is what I did for validating only a domain name like "my.domain". I have to enter a "." in the input field or else it fails, I just wish the red validation would cooperate :/ Thanks.
@LUISEGONZALEZL
@LUISEGONZALEZL 4 жыл бұрын
If I have an integer type attribute but the value entered by the user is greater than the maximum value allowed for integers, how do I validate it and display a custom message?
@payalagarwal8564
@payalagarwal8564 4 жыл бұрын
Sir, When i am defining [ValidEmailDomain] attribute in RegisterViewModel class and press [ctrl .] then it is not recognising EmployeeManagement.Utilities namespace ?
@misaghsh9517
@misaghsh9517 3 жыл бұрын
for sure you have misspelling or typo error. Check and make sure you type class name correctly.
@mohammadmkhani105
@mohammadmkhani105 3 жыл бұрын
thank you
@kunalshah7270
@kunalshah7270 4 жыл бұрын
Kudvenkat Sir, I have done exact replications of the steps that you have shown in video still my IsValid function is getting called only on click of Submit nor I have any compile time errors, what might be the reason? Can you please help?
@lifetraveler8008
@lifetraveler8008 4 жыл бұрын
Did you find the solution? I am also having the same issue
@kunalshah7270
@kunalshah7270 4 жыл бұрын
@@lifetraveler8008 No sir not yet.
@naodagere8210
@naodagere8210 4 жыл бұрын
Thank you sir.
@manishk009m
@manishk009m 5 жыл бұрын
Thanks for great explanation, Please create a video on client side custom validation as well.
@Csharp-video-tutorialsBlogspot
@Csharp-video-tutorialsBlogspot 5 жыл бұрын
Hello Manish - Client side validation is covered in details in Parts 74 to 76. Here is the link to complete ASP.NET Core MVC tutorial. www.pragimtech.com/courses/asp-net-core-mvc-tutorial-for-beginners/
@vinothdharmaraj7510
@vinothdharmaraj7510 5 жыл бұрын
@@Csharp-video-tutorialsBlogspot No venkat, this custom attribute only working on server side only and we should write for client side also.
@marconagel3018
@marconagel3018 4 жыл бұрын
@@vinothdharmaraj7510 Taking into consideration that maybe you want 2 remote validations for the same field that is email, .net core doesnt let you add multiple remote attributes so, you could create a method IsEmailValid and check within if the email is in use or if the email match with domain "PREGIM.COM" like Lin mentioned here stackoverflow.com/questions/20329121/multiple-remote-validation-attribute-in-asp-net-mvc4. Hope it helps you Vinoth, Venkat I admire you everyday, thanks for giving us free education for future engineers or good programmers, regards..
@stdcarriers693
@stdcarriers693 4 жыл бұрын
This video concludes saying it works "just like any other validation attribute" that is not true. There is no client side validation, so the form must post before the attribute runs. That is problematic if the user has filled out form fields that depend on dynamic controls because they will lost their data.
@seneysean
@seneysean 5 жыл бұрын
What about custom validations for client and remote?
@ransack9385
@ransack9385 4 жыл бұрын
Hi, Anyone can you help. For Some reason the IsValid Method doesnt fire for me. I have no Compile time errors. Any ideas?
@kunalshah7270
@kunalshah7270 4 жыл бұрын
Sir even my IsValid overridden function is not getting executed, did you found the reason? Please share.
@Abhimanyukumar-vb8bz
@Abhimanyukumar-vb8bz 4 жыл бұрын
@@kunalshah7270 what is happening can you elaborate
@rmm1332
@rmm1332 3 жыл бұрын
Did you get any solution to this? I haven't try it yet but I have high hope for it to works 😅 seeing this message making the hope seems a bit dimmer
@robertmays6097
@robertmays6097 5 жыл бұрын
great
@МаркВирченко-ж6щ
@МаркВирченко-ж6щ 5 жыл бұрын
wouldnt that be much easier to just call value.EndsWith(allowedDomain)?
@МаркВирченко-ж6щ
@МаркВирченко-ж6щ 5 жыл бұрын
and safer
@roy_antony
@roy_antony 4 жыл бұрын
Don't think so. ex: allowed domain is "mail.com".....I can add "gmail.com". It is better to split the string with '@'.
@steveymcneckbeard
@steveymcneckbeard 5 жыл бұрын
Hi Venkat, will you be covering Fluid Validation in this series?
@loveunimeanit
@loveunimeanit 5 жыл бұрын
how can you implement custom attribute on client side ????
@genovme
@genovme 4 жыл бұрын
How can I allowed more than one domain?
@abderrahmanrahou9189
@abderrahmanrahou9189 4 жыл бұрын
Hi, to allow more than one domain you should pass an Array of string, and this is code source: for RegisterViewModel class: [RegisterEmailDomainValidation(AllowedDomain: new string[] {"gmail.com","hotmail.com"} ,ErrorMessage ="This email domain should be: gmail.com or hotmail.com")] public string Email { get; set; } --------------------------------------------------------------------------------------------------------------------------------------------------------- For ValideEmailAttribute class: public class ValideEmailAttribute : ValidationAttribute { private readonly string[] AllowedDomain; public ValideEmailAttribute (string[] AllowedDomain) { this.AllowedDomain = AllowedDomain; } public override bool IsValid(object value) { string[] splitedEmail = value.ToString().Split("@"); foreach(var v in AllowedDomain) { if(v.ToUpper()==splitedEmail[1].ToUpper()) { return true; } } return false; } } Hope this code is helpful, have a nice chance.
@javaguitarist
@javaguitarist 4 жыл бұрын
@@abderrahmanrahou9189 Thank you kindly for this idea. I have a tested the following solution. VALIDEMAILDOMAINATTRIBUTE public class ValidEmailDomainAttribute : ValidationAttribute { private readonly string[] allowedDomain; public ValidEmailDomainAttribute(string[] allowedDomain) { this.allowedDomain = allowedDomain; } public override bool IsValid(object value) { foreach (var dom in allowedDomain) { string[] emailDomain = value.ToString().Split("@"); string thisEmailDomain = emailDomain[1]; string domElement = dom.ToString().ToUpper(); if (thisEmailDomain.ToString().ToUpper() == domElement.ToUpper()) { return thisEmailDomain == dom; } } return false; } } REGISTERVIEWMODEL public class RegisterViewModel { public string City { get; set; } [Required] [EmailAddress] [Remote(action: "IsEmailInUse", controller: "Account")] [ValidEmailDomain(allowedDomain: new string[] { "abc.com", "xyz.com" }, ErrorMessage = "Email domain must be abc.com or xyz.com.")] public string Email { get; set; } [Required] [DataType(DataType.Password)] public string Password { get; set; } [DataType(DataType.Password)] [Display(Name ="Confirm Password")] [Compare("Password", ErrorMessage ="Password and confirmation do not match.")] public string ConfirmPassword { get; set; } } }
@weichengchen4598
@weichengchen4598 5 жыл бұрын
This is how you do client side validations for custom attribute. stackoverflow.com/questions/41900485/custom-validation-attributes-comparing-two-properties-in-the-same-model
@hossammetwally07
@hossammetwally07 5 жыл бұрын
Why should we use ToUpper() function?
@IamSandeepKmr
@IamSandeepKmr 5 жыл бұрын
What if the domain name you want to validate with is pragimtech.com and user enters Pragimtech.com. The validation will fail but it is not advisable to make it case sensitive comparison thats the reason.
@hossammetwally07
@hossammetwally07 5 жыл бұрын
@@IamSandeepKmr Thanks bro
@jeffreyhermosa
@jeffreyhermosa 5 жыл бұрын
Video thumbnail is wrong, but as always great video
@javaguitarist
@javaguitarist 4 жыл бұрын
Once I found myself unwittingly 10 videos ahead of the next one, so I think this happened before also.
@Suecia2020
@Suecia2020 4 жыл бұрын
Thank you sir.
Extend IdentityUser in ASP NET Core
10:52
kudvenkat
Рет қаралды 103 М.
Model Validation | Validation Attributes vs. FluentValidation | .NET 6
27:11
Арыстанның айқасы, Тәуіржанның шайқасы!
25:51
QosLike / ҚосЛайк / Косылайық
Рет қаралды 700 М.
REAL or FAKE? #beatbox #tiktok
01:03
BeatboxJCOP
Рет қаралды 18 МЛН
How To Create Smart Enums in C# With Rich Behavior
17:31
Milan Jovanović
Рет қаралды 58 М.
ASP NET Core dependency injection tutorial
9:28
kudvenkat
Рет қаралды 436 М.
Elegant Global Error Handling Using Middleware In ASP.NET Core
13:58
Milan Jovanović
Рет қаралды 92 М.
(#50) Model validations in asp.net core | Asp.Net Core tutorial
20:21
Арыстанның айқасы, Тәуіржанның шайқасы!
25:51
QosLike / ҚосЛайк / Косылайық
Рет қаралды 700 М.