asp.net mvc 5 - requiredif on an element in a list of custom inputs -


i have modelview contains list of icustominput values

public class demoviewmodel {     [required]     public string firstname {get; set;}     [required]     public string lastname {get; set;}      [requiredif("dayphonerequired", true)]     public string dayphone {get; set;}     public bool dayphonerequired {get; set;} = false;     public list<icustominput> custominputfields { get; set; } = new list<icustominput>(); } 

an example of icustominput

public class customtextinput : icustominput     {         public customfield field { get; }         public string displayname { get; set; }          [requiredif("datavaluerequired", true, errormessage = "this required field")]         public virtual string datavalue { get; set; }         public bool datavaluerequired { get; set; } = false;         public virtual string classname => "customtextinput";         public string assemblyname => "application.models";     } 

the purpose of can pull information db custom input fields logged in client has requested on form. 1 client may want couple text fields, client may want drop down. these custom fields may or may not require input well. (the customfield object older object returned datalayer , used heavily, don't want rebuild it, assume it's full of strings)

i have editor template concrete implementations of icustominputs custom binders allow me data on post. issue i'm having requiredif attribute setting unobtrusive data values client side validation same icustominputs. makes sense since have same name dependent property, doesn't solve issue have.

my view displays list of icustominput simply:

@html.editorfor(model => model.custominputfields) 

then each concrete type implements icustominput has it's own editortemplate similar to:

<div class="columnpositioner">     <div class="inputcontainer">         @html.textboxfor(model => model.datavalue, new    {        @class = "inputfields input-lg form-control",        placeholder = model.field.display    })         <span class="inputlabel">             @html.labelfor(model => model.field.display, model.field.display)         </span>         @html.validationmessagefor(model => model.datavalue, "", new { @class = "text-danger" })         @html.hiddenfor(model => model.datavaluerequired)     </div> </div> 

the resulting html looks like:

<select name="custominputfields[0].datavalue" class="inputfields input-lg form-control" id="custominputfields_0__datavalue" data-val="true" data-val-requiredif-operator="equalto" data-val-requiredif-dependentvalue="true" data-val-requiredif-dependentproperty="datavaluerequired" data-val-requiredif="this required field"><option value="">test01</option> <option value="01">01</option> <option value="02">02</option> <option value="03">03</option> </select> <input name="custominputfields[0].datavaluerequired" class="hascontent" id="custominputfields_0__datavaluerequired" type="hidden" value="true" data-val-required="the datavaluerequired field required." data-val="true"> <input name="custominputfields[1].datavalue" class="inputfields input-lg form-control" id="custominputfields_1__datavalue" type="text" placeholder="test02" value="" data-val="true" data-val-requiredif-operator="equalto" data-val-requiredif-dependentvalue="true" data-val-requiredif-dependentproperty="datavaluerequired" data-val-requiredif="this required field"> <input name="custominputfields[1].datavaluerequired" id="custominputfields_1__datavaluerequired" type="hidden" value="false" data-val-required="the datavaluerequired field required." data-val="true"> 

the hidden field named properly, how can attribute set data-val-requiredif-dependentproperty actual id/name on hidden field?

i not have custom editor template list. did have one, couldn't bind data correctly. dropping editor template on list , building unique editor templates concrete implementations of icustominput gave me ui layout control needed , bound data correctly, can't client side validation work properly. if it's editor template, might like?

update

this a fix, don't it. have javascript that's doing .each through inputs apply styles added .each:

function requiredifhack($input) {     var deppropval = $input.data("val-requiredif-dependentproperty");     //return if value exists     if ($("#" + deppropval).length) return;      //it doesn't. it's missing parent object name     var parentname = $input.attr("name").split(".")[0].replace("[", "_").replace("]", "_");     $input.data("val-requiredif-dependentproperty", parentname + "_" + deppropval); } 

it solves problem, don't think should problem js responsibility solve. , since it's pretty sneaky fix, trip others trying work on code in future. still want find better way it.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -