asp.net mvc - How to get unobtrusive client-side validation on a Required List of objects in MVC? -
I have a model that has a list of the functions of an employee, the employee must have at least one function.
public class employee {[required (error message = "name is required")) public string name {get; Set; } [Required (error message = "email is required")] [Regular expression (@ "^ ([a-zA-Z0- 9 _ \ - \.] +) @ ((\ [[0- 9] {1 , 3} "+ @". [0- 9] {1,3}. [0- 9] {1,3}.) | (([A-zA-Z0-9 \ -] + \ "+ @ ".) +)) ([A-zA-Z] {2,4} | [0- 9] {1,3}) (\??) $", Error message = "email is not valid")] Public string email {get; Set; } [Required (error message = "at least one function is required")] Public listing & lt; Function & gt; Work {received; Set; }} Public class function {[required (error message) "name is required")] Public string name {get; Set; }} I have created an EditorTemplate for a function
@model MvcClientSideValidation.Models.Function & lt; Fieldet & gt; & Lt; Story & gt; Function & lt; / Narrative & gt; & Lt; Div class = "editor-label" & gt; @ Html.LabelFor (model => model.Name) & lt; / Div & gt; & Lt; Div class = "editor-field" & gt; @ Html.EditorFor (model = & gt; model.Name) @ html.ValidationMessageFor (model = & gt; model.Name) & lt; / Div & gt; & Lt; / Fieldset & gt; Uses an validation message for the index view editor.
@ html.EditorFor (m => m.functions) @ html.ValidationMessageFor (m = & gt; m.functions) function Includes the code to add or remove functions.
When the view is submitted, the client does not check side verification, if a function exists, the server side verifies the problem. The problem is that when the list is empty, then function No input element is provided for the property, so there is no tag in which verification tags can be added. That is why I am looking for an easy way to perform ambiguous client-side verification for the [required] attribute with the list . Edit: I just realized [required] attribute probably will only confirm that function is not null . It will not check whether any items are included in it or not. It's okay by me, because the property will automatically become redundant on postbacks.
Try it out:
[required number (error message = " At least one function is required ")] [UIHint (" function combination ")] // - & gt; The EditorTemplate partial view ensures that the input is generated. Public listing & lt; Function & gt; Work {received; Set; } Required Number Other Category: Public class required essential characteristic: Validation, = IEnumerable as value; If (countable == zero) return false; IEnumerator enumerator = enumerable.GetEnumerator (); If (counters. Headbook ()) come true; return false; } Public IEnumerable & lt; ModelClientValidationRule & gt; GetClientValidationRules (ModelMetetadata Metadata, Controller Contact Reference) {yield returns new model client validity rule {validation type = "expected required", error message = error message string}; }} Function Collection Partial View (/Views/Shared/EditorTemplates/FunctionCollection.cshtml):
@Emelumerable & lt; Function & gt; @ {Function [] model = model! = Null? Model as the function [] ?? Model ToArray (): New Function [0]; String value = NewTonsoft. Jason Jason Convert. Serialize object (model selection (x = & gt; x.Name)); } @ Html.Hidden (string.opti, value) @html Validity message (string.opti) @ if (model.ne.nani ()) {& lt; Ul & gt; @Farach (Function F in Models) {& lt; Li & gt; @famnam & lt; / Li & gt; } & Lt; / Ul & gt; } @ Ajax.ActionLink ("edit function", "index", "function", new {selectedFunctions = value}, new application {} in JavaScript) : < Script> gt; pre & lt; $ .validator (before $$) $ .validator.addMethod ("required numbernumber", function (value) {value = $ .trim (value); if (! Value .length) false return; // This value assumes that it is either a string of values separated by a Jason array or comma? Var arr = value.indexOf ("["] === 0? $ ParseJSON (Value): Value.split (","); Return arr & arr.length> 1;}); $ .validator.unobtrusive.adapters.addBool ("requiredenumerable"); // like # function input is hidden, so we need to customize the validator object (within $.) $ (Under document) $ (document) .ready (function () {var elem selector = "#functions "; Var validator = $ (elemSelector) .closest (" form "). Data (" validator "); if (validator) {validator.settings.ignore =": hidden: no ("+ elemSelector +") "; }}); & Lt; / Script & gt;
Comments
Post a Comment