IDateErrorInfo
IDateErrorInfo is supported by the MVC environment (Microsoft tutorial can be found here ). By default, the middleware will be responsible for re-creating the model objects by associating the html form elements with the model. If the model binder detects that the model implements the interface, then it will use the interface methods to check each property in the model or to check the model as a whole. See the manual for more information.
If you want to use client-side validation using this method, then (to quote Steve Sanderson) "the most direct way to use additional validation rules is to manually create the necessary attributes in the view":
<p> @Html.TextBoxFor(m.ClientName, new { data_val = "true", data_val_email = "Enter a valid email address", data_val_required = "Please enter your name"}) @Html.ValidationMessageFor(m => m.ClientName) </p>
This can then be used to run any client-side validation that has been defined. The following is an example of how to define client-side validation.
Explicit Verification
As you mentioned, you could demonstrate the model in action. For instance:
public ViewResult Register(MyModel theModel) { if (theModel.PropertyB < theModel.PropertyA) ModelState.AddModelError("", "PropertyA must not be less then PropertyB"); if (ModelState.IsValid) { //save values //go to next page } else { return View(); } }
In the view, you will need to use @Html.ValidationSummary to display the error message, since the code above will add a model level error, not a property level error.
To indicate a property level error, you can write:
ModelState.AddModelError("PropertyA", "PropertyA must not be less then PropertyB");
And then in the view use:
@Html.ValidationMessageFor(m => m.PropertyA);
to display an error message.
Again, any client-side validation should be linked by manually binding in the client-side validation in the view by defining properties.
Model Validation Custom Attribute
If I understand the problem correctly, you are trying to test a model containing a single value and a collection in which the property in the collection should be summed.
In the example I will give, the view will present the user with a maximum value field and 5 value fields. The maximum value field will be the only value in the model, where, since 5 value fields will be part of the collection. Validation ensures that the sum of the value fields is not larger than the maximum value field. Validation will be defined as an attribute of the model, which will also bind well with valdation on the client side of javascript.
View:
@model MvcApplication1.Models.ValueModel <h2>Person Ages</h2> @using (@Html.BeginForm()) { <p>Please enter the maximum total that will be allowed for all values</p> @Html.EditorFor(m => m.MaximumTotalValueAllowed) @Html.ValidationMessageFor(m => m.MaximumTotalValueAllowed) int numberOfValues = 5; <p>Please enter @numberOfValues different values.</p> for (int i=0; i<numberOfValues; i++) { <p>@Html.EditorFor(m => m.Values[i])</p> } <input type="submit" value="submit"/> }
I have not added any validation against value fields, as I do not want to overly complicate this example.
Model:
public class ValueModel { [Required(ErrorMessage="Please enter the maximum total value")] [Numeric]
Actions:
public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(ValueModel model) { if (!ModelState.IsValid) { return View(model); } else { return RedirectToAction("complete");
User attribute:
The [ValuesMustNotExceedTotal] attribute defined in the model can be defined by overriding the ValidationAttribute class:
public class ValuesMustNotExceedTotalAttribute : ValidationAttribute { private int maxTotalValueAllowed; private int valueTotal; public ValuesMustNotExceedTotalAttribute() { ErrorMessage = "The total of all values ({0}) is greater than the maximum value of {1}"; } public override string FormatErrorMessage(string name) { return string.Format(ErrorMessageString, valueTotal, maxTotalValueAllowed); } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { PropertyInfo maxTotalValueAllowedInfo = validationContext.ObjectType.GetProperty("MaximumTotalValueAllowed"); PropertyInfo valuesInfo = validationContext.ObjectType.GetProperty("Values"); if (maxTotalValueAllowedInfo == null || valuesInfo == null) { return new ValidationResult("MaximumTotalValueAllowed or Values is undefined in the model."); } var maxTotalValueAllowedPropertyValue = maxTotalValueAllowedInfo.GetValue(validationContext.ObjectInstance, null); var valuesPropertyValue = valuesInfo.GetValue(validationContext.ObjectInstance, null); if (maxTotalValueAllowedPropertyValue != null && valuesPropertyValue != null) { bool maxTotalValueParsed = Int32.TryParse(maxTotalValueAllowedPropertyValue.ToString(), out maxTotalValueAllowed); int dummyValue; valueTotal = ((List<string>)valuesPropertyValue).Sum(x => Int32.TryParse(x, out dummyValue) ? Int32.Parse(x) : 0); if (maxTotalValueParsed && valueTotal > maxTotalValueAllowed) { return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName)); } }
Adding client-side validation to a user attribute:
To add client-side validation to this attribute, you must implement the IClientValidatable interface:
public class ValuesMustNotExceedTotalAttribute : ValidationAttribute, IClientValidatable {
If you were to start the application at this moment and view the source html for the field defining the attribute, you will see the following:
<input class="text-box single-line" data-val="true" data-val-number="The MaximumTotalValueAllowed field is not a valid number." data-val-required="Please enter the maximum total value" data-val-valuesmustnotexceedtotal="The total of all values is greater than the maximum value." id="MaximumTotalValueAllowed" name="MaximumTotalValueAllowed" type="text" value="" />
In particular, pay attention to the validation attribute data-val-valuesmustnotexceedtotal . Therefore, our client-side validation will reference the validation attribute.
Adding client-side validation:
To add client-side validation, you need to add the following library tags to the presentation tag:
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
You also need to make sure that client-side validation is enabled in the web.config file, although I think this should be enabled by default:
<add key="ClientValidationEnabled" value="true"/> <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
All that remains is to define client-side validation in the view. Please note that the added validation is defined in the view, but if it was defined in the library, then a custom attribute (maybe not this one) can be added to other models for other views:
<script type="text/javascript"> jQuery.validator.unobtrusive.adapters.add('valuesmustnotexceedtotal', [], function (options) { options.rules['valuesmustnotexceedtotal'] = ''; options.messages['valuesmustnotexceedtotal'] = options.message; }); //note: this will only be fired when the user leaves the maximum value field or when the user clicks the submit button. //i'm not sure how you would trigger the validation to fire if the user leaves the value fields although i'm sure its possible. jQuery.validator.addMethod('valuesmustnotexceedtotal', function (value, element, params) { sumValues = 0; //determine if any of the value fields are present and calculate the sum of the fields for (i = 0; i <= 4; i++) { fieldValue = parseInt($('#Values_' + i + '_').val()); if (!isNaN(fieldValue)) { sumValues = sumValues + fieldValue; valueFound = true; } } maximumValue = parseInt(value); //(if value has been supplied and is numeric) and (any of the fields are present and are numeric) if (!isNaN(maximumValue) && valueFound) { //perform validation if (sumValues > maximumValue) { return false; } } return true; }, ''); </script>
And that should be so. I am sure that there are improvements that can be made here and there, and that if I misunderstood the problem, you should improve the validation for your needs. But I believe that this check seems to be such that most developers code custom attributes, including a more sophisticated client-side check.
Hope this helps. Let me know if you have any questions or suggestions regarding the above.