This project is read-only.

RequiredIfTrue not working with EditorTemplates


RequiredIfTrue doesn't work with EditorTemplates.
if have a list of RegistrationStepFields (List<RequiredStepModel>) wich a have an EditorTemplate for.
@Html.EditorFor(x => x.RegistrationStepFields, ViewData).
In my model i have bound the RequiredIf Attribute to the Value field like this:
public bool Required { get; set; }
public string Value { get; set; }
the problem is that the View is rendered like this:
<input type="hidden" value="True" name="RegistrationStepFields[1].Required" id="RegistrationStepFields_1__Required" data-val-required="The Required field is required." data-val="true" class="valid">
<input type="text" value="" name="RegistrationStepFields[1].Value" id="RegistrationStepFields_1__Value" data-val-requiredif-operator="EqualTo" data-val-requiredif-dependentvalue="True" data-val-requiredif-dependentproperty="Required" data-val-requiredif="Value is required due to Required being equal to True" data-val-remote-url="/Register/ValidateAlias" data-val-remote="Profile_Alias is valid" data-val="true" tabindex="1" class="dating-forminput dating-forminput-10 tooltip-enabled Profile_Alias valid" data-original-title="blabla">
so it seems like the foolproof validation is not aware of validation within a loop (EditorTemplate) so the validation is searching for a property wich doesn't exist.


nickriggs wrote Apr 12, 2012 at 3:42 PM

I want to make sure we are seeing the same result. In the sample I threw together, the client-side is validating but not displaying an error. Is that what you are seeing?

He is my sample:

wrote Jul 9, 2012 at 3:37 PM

zbarrier wrote Nov 17, 2012 at 1:25 AM


I have this same problem. My html contains the correct markup, but client side validation doesn't work. Have you had any luck in solving this?

wrote Feb 22, 2013 at 12:13 AM

wrote May 14, 2014 at 4:31 PM

wrote Oct 31, 2014 at 12:36 AM

sleeyuen wrote Oct 31, 2014 at 12:47 AM

Complex ids/names seems to be the issue. We're having a similar problem in our project since the controls are being added to the page in a loop, so the ids end up with underscores, much like OP's example. Any headway on this issue, by chance?

sleeyuen wrote Oct 31, 2014 at 7:28 PM

Found a workaround. In the addMethod for requiredif, we changed the following:
a = document.getElementById(c.dependentproperty)
a = document.getElementsByName(h)
if (a.length > 1) { for (var d = 0; d != a.length; d++) if (a[d].checked) { b = a[d].value; break } if (b == null) b = false } else b = a[0].value;
b = a.value;
Note, however, that this will break validation on radio button controls, or any controls that share the same name (name="shared_name"). We don't implement radio buttons (we prefer dropdowns), so this isn't an issue for us.

Hope this helps someone.