.NET 4 MVC 2 Validation with annotations warning instead of error
I am using .NET 4 with MVC 2 for validating with Annotations. Is there a (simple) solution for giving back a warning instead of the error? So that I am able to get a green or yellow box with a message like "you should not use this content, but you may".
Big thanks in advance! :)
EDIT:
Please observe that I am already able to throw out errors viaErrorMessage
but I additionally want something like WarningMessage
or InfoMessage
so that the user only gets a warning but might proceed. Is there a solution for this?
The Pseudocode would be:
(Please note the "pseudo", because WarningMessage
is (unfortunately) not a valid class)
public class Person
{
[StringLength(50)]
[Required(ErrorMessage = "You MUST enter a name!")]
public string Name { get; set; }
[Required(WarningMessage = "It is recommended to fill out the age but you may leave it out)]
public int Age { get; set; }
}
And yes I want to have this centrallized in my validation class - not somewhere in a .js-fi开发者_高级运维le.
I will first quickly comment that I'm not sure if this is a good idea!
"Required" attribute has a certain "character" - people expect certain things of it when they include this on their attributes. If you want to use attributes to define this behaviour, try to describe what you're doing more exactly, so instead of your:
[Required(WarningMessage = "It is recommended to fill out age...")]
public int Age { get; set; }
I'd have:
[PreSubmitWarningMessage("It is recommended to fill out age...")]
public int Age { get; set; }
You can create your own attributes that'll affect how the request is handled, though this won't propagate to client-side validation like the standard MVC attributes do.
To get this to play nice with MVC, you need to create this as an Action Filter - specifically by implementing the ActionFilterAttribute and overriding the OnActionExecuted method:
public class PreSubmitWarningMessage : ActionFilterAttribute
{
private string _warningMessage;
public PreSubmitWarningMessage(string warningMessage)
{
_warningMessage = warningMessage;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// stuff you do here will happen before the
// controller action is executed.
// you can compare the private warning message
// to the metadata in filterContext
}
}
You could do a couple of things in here, I'm not sure what best practice would be, but at the hackiest, you have access to the model in filterContext, so you can change the behaviour of your controller action, updating your view model to a certain state if your warning condition (for this case, that the field is required).
People might make a case for just extending the RequiredAttribute, but I don't think it's correct to say that your new Attribute IS a RequiredAttribute, so inheritence wouldn't be conceptually correct.
I'm not sure how this would even work. If the user doesn't enter the data, and the data is accepted, then you would go on to some other page and they would never see the warning message. This would mean you would either have to not progress, showing them the warning (and then requiring them to do something else to progress) or you would have to pop up a dialog or alert requiring them to approve going on.
Neither is a very good solution from a usability standpoint.
How do you propose the user actually see the warning?
I am not sure that DataAnnotations will help in this regard. You can always use custom client side validations. Create a custom class to pass error message to client
I dont think there is any proper need to use annotations and validate data using it here. The best thing to do here is to may be write a JS function that shows a tool-tip type of message when the user gives some input that fails some input condition, as you are anyways going to allow it. The basic purpose of data annotations is to not allow inputs that fails the defined conditions to reach the db.
All validation Attributes have ErrorMessage property. You can give your custom error message
public class Product
{
[StringLength(50)]
[Required(ErrorMessage = "Your user friendly message")]
public string Name { get; set; }
}
Then change the CSS styles of the error messages.
/* Styles for validation helpers
-----------------------------------------------------------*/
.field-validation-error
{
color: #ff0000;/*change the color*/
}
/*Other validation related styles*/
精彩评论