Add Error To Validation Summary
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn add error to validationsummary from code behind more about Stack Overflow the company Business Learn more about hiring developers or jquery add error message to validation summary posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow
C# Validationsummary Add Message
Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Add error message to @Html.ValidationSummary up vote 19 down
Add Message To Validation Summary Using Javascript
vote favorite 7 I'm using standard MVC3 Razor views with unobtrusive Javascript validation, using @Html.ValidationSummary to show them at the top of the form. If the standard validations (things like [Required]) pass, I then run some very custom client-side validation that fires when the user hits the Submit button. (The validation looks across a number of form elements to make sure that the proper set of them have asp net validation summary custom message been checked, etc., so it's not as simple as just creating a new custom validator for a single field). I'd like the possible error(s) I construct there to be shown in the ValidationSummary list, but I can't figure out how to get the error message to appear there. asp.net-mvc-3 unobtrusive-validation share|improve this question edited Aug 22 '12 at 9:08 Eranga 26.3k36478 asked Oct 24 '11 at 21:10 Kras 2432612 In client-side or server-side? –Javad_Amiry Oct 24 '11 at 21:16 add a comment| 2 Answers 2 active oldest votes up vote 32 down vote In client-side: function YourCustomValidator() { // do your validation logic here via JavaScript return true; // or false based on your validation logic } $(document).ready(function () { // take your own form-selector like ("form", this) $("form", this).first().submit(function () { return (YourCustomValidator() && $(this).valid()); }); }); OR In server-side: Think you have a model like this: public class Test { [Required] [StringLength(100)] public string FullName { get; set; } } and when you are validating it: if(ModelState.IsValid) { // default validations run here if(/* some custom validations run here, there is an error about "FullName" */){ // you should set the "key" for Model-Error to "FullName" ModelState.AddModelError("FullName","error-messag
J InceFebruary 28, 200819 0 0 0 For a while now I’ve used this handy bit of code to add a message programmatically to a Validation Summary control, without associating it with a Validator. I’ve no idea where it
Validationsummary Not Showing Error Messages
came from – perhaps my head, perhaps someone cleverer than I… so if it validationsummary not showing error messages mvc was from you, shout up! I was asked how to do this today by a customer, so I felt inspired to validationsummary add message programmatically blog it. Anyway, sometimes you get an error from your business logic that it just isn’t practical to have pre-validated. For example, when adding a new employee to a database, perhaps the employee name http://stackoverflow.com/questions/7882008/add-error-message-to-html-validationsummary has a UNIQUE constraint on it. Validating this up front might not be easy… So if I get an error back from my business logic (either in the form of a list of validation errors, or in the worst case scenario as an exception) how do I display this message to the user? Well it turns out this is quite easy – just add a validator that is https://blogs.msdn.microsoft.com/simonince/2008/02/28/adding-messages-to-a-validation-summary/ reporting itself as “IsValid = false” to the Page.Validators collection. Consider the following class; public class ValidationError : IValidator { private ValidationError(string message) { ErrorMessage = message; IsValid = false; } public string ErrorMessage { get; set; } public bool IsValid { get; set; } public void Validate() { // no action required } public static void Display(string message) { Page currentPage = HttpContext.Current.Handler as Page; currentPage.Validators.Add(new ValidationError(message)); } } (Note: This is usingautomatic properties - a C# 3.0 feature. Alter the code to use standard properties if you're using an earlier version of .NET) This immediately allows me to use the following code; ValidationError.Display("Oops, some error occurred."); Succinct, eh?! Here’s a shot of it in action;
Tags ASP.NET C# Comments (19) Cancel reply Name * Email * Website Jason says: February 29, 2008 at 12:07 pm Thanks for this. It definitely came in useful for me. Reply KA says: March 28, 2008 at 1:15 pm This works well - thanks for the tip. Reply Nicole says: April 7, 2008 at 2:06 pm SometPosted on July 4, 2014 by briancaos This trick is especially useful when you have custom https://briancaos.wordpress.com/2014/07/04/add-custom-text-to-aspvalidationsummary-c/ code to be executed after your form have been http://justgeeks.blogspot.com/2010/11/adding-item-to-validationsummary.html submitted, and still wishes to communicate an error the same way as you communicate form validation errors. Imagine the following ValidationSummary:
In general, field specific validation shows up next to a control that it is validating (assuming you put the validators next to it). But what about validation that happens in a Domain Service Class or your Custom BLL for example? These exceptions will by default be caught by the application and show as a nasty error to the user, or go to the error page. This is hardly the desired behavior for a validation error. First I like to change the default behavior of bubbling up to the application to be caught to being handled at the button or page level. To do this I put a try-catch in my button action or other applicable event that you can tap into. In the catch, it would be ideal to add a custom error message to the ValidationSummary. How do we do that though? Thankfully, it is quite easy to add an item to the ValidationSummary. The key is that the Page has a Validators property that all validators are automatically added to when you put them on your .aspx page. The problem is that we don't have a CustomValidator. Thus we need to create a CustomValidator, but what a pain really since we only want to use it when we actually have an exception in our BLL. My solution is to create method to encapsulate the logic to create a new CustomValidator and add it to the Page's Validators collection. So that it can easily be accessed on any page, I have implemented it as an Extension to the Page class. Below is the code to do so. namespace MyExtensions { public static class PageExtensions { public static void AddValidationSummaryItem(this Page page, string errorMessage) { var validator = new CustomValidator(); validator.IsValid = false; validator.ErrorMessage = errorMessage; page.Validators.Add(validator); } } } To use this method just put the using MyExtensions; statement at the top of your code-behind