MVC 3 Complex Model Verification

The current validation method for use in MVC 3 seems to be ValidationAttributes. I have a class check that is very specific to this model and has interactions between several properties.

Basically, a model has a collection of other models, and they are edited in the same form. Let me call him ModelA, and he has a collection of ModelB. One thing that I might need to check out is that the sum of some ModelB property is less than PropertyA. The user has X the number of points that he can divide among some options.

ValidationAttributes are very general, and I'm not sure if they are suitable for this assignment.

I don't know how IDateErrorInfo is supported in MVC 3 and whether it works right out of the box.

One way is to validate using the method, but that means that I cannot perform client validation.

What is the right way to do something like this? Are there any other options that I have? Am I underestimating the power of ValidationAttribute?

+4
source share
4 answers

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] //using DataAnnotationExtensions [ValuesMustNotExceedTotal] public string MaximumTotalValueAllowed { get; set; } public List<string> Values { get; set; } } 

Actions:

 public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(ValueModel model) { if (!ModelState.IsValid) { return View(model); } else { return RedirectToAction("complete"); //or whatever action you wish to define. } } 

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)); } } //if the maximum value is not supplied or could not be parsed then we still return that the validation was successful. //why? because this attribute is only responsible for validating that the total of the values is less than the maximum. //we use a [Required] attribute on the model to ensure that the field is required and a [Numeric] attribute //on the model to ensure that the fields are input as numeric (supplying appropriate error messages for each). return null; } } 

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 { //...code as above... //this will be called when creating the form html to set the correct property values for the form elements public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { var rule = new ModelClientValidationRule { ValidationType = "valuesmustnotexceedtotal", //the name of the client side javascript validation (must be lowercase) ErrorMessage = "The total of all values is greater than the maximum value." //I have provided an alternative error message as i'm not sure how you would alter the {0} and {1} in javascript. }; yield return rule; //note: if you set the validation type above to "required" or "email" then it would use the default javascript routines (by those names) to validate client side rather than the one we define } } 

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.

+5
source

This is what you are looking for:

http://www.a2zdotnet.com/View.aspx?Id=182

+1
source

Your model class can implement the IValidatableObject interface.

Thus, you have access to all the properties of your model class and you can perform all your own checks.

You also have an IClientValidatable interface for client-side validation, but I'm not sure that by implementing it directly in the model class, client validations are selected by MVC, since I used this interface only to specify client validation in user verification attributes.

+1
source

I also had a similar situation, I need to compare the value of property A with property B, and I do this:

  public sealed class PropertyAAttribute : ValidationAttribute { public string propertyBProperty { get; set; } // Override the isValid function public override bool IsValid(object value) { // Do your comparison here, eg: return A >= B; } } 

Then just use the custom validation attribute as follows:

  [PropertyA(propertyBProperty = "PropertyB")] public string Property A {get; set;} 

I also tried very hard and received this solution from others, I hope this help!

+1
source

Source: https://habr.com/ru/post/1379279/


All Articles