Note that there are some explanatory texts on larger screens.

plurals
  1. POValidation of objects in ViewModel not adding CSS validation classes
    primarykey
    data
    text
    <p>I have the following view models:</p> <pre><code>public class Search { public int Id { get; set; } [Required(ErrorMessage = "Please choose a name.")] public string Name { get; set; } [ValidGroup(ErrorMessage = "Please create a new group or choose an existing one.")] public Group Group { get; set; } } public class Group { public int Id { get; set; } public string Name { get; set; } } </code></pre> <p>I have defined a custom validation attribute as follows:</p> <pre><code>public class ValidGroupAttribute : ValidationAttribute { public override bool IsValid(object value) { if (value == null) return false; Group group = (Group)value; return !(string.IsNullOrEmpty(group.Name) &amp;&amp; group.Id == 0); } } </code></pre> <p>I have the following view (omitted some for brevity):</p> <pre><code> @Html.ValidationSummary() &lt;p&gt; &lt;!-- These are custom HTML helper extensions. --&gt; @Html.RadioButtonForBool(m =&gt; m.NewGroup, true, "New", new { @class = "formRadioSearch", id = "NewGroup" }) @Html.RadioButtonForBool(m =&gt; m.NewGroup, false, "Existing", new { @class = "formRadioSearch", id = "ExistingGroup" }) &lt;/p&gt; &lt;p&gt; &lt;label&gt;Group&lt;/label&gt; @if (Model.Group != null &amp;&amp; Model.Group.Id == 0) { @Html.TextBoxFor(m =&gt; m.Group.Name) } else { @Html.DropDownListFor(m =&gt; m.Group.Id, Model.Groups) } &lt;/p&gt; </code></pre> <p>The issue I'm having is the validation class <code>input-validation-error</code> does not get applied to the Group input. I assume this is because the framework is trying to find a field with <code>id="Group"</code> and the markup that is being generated has either <code>id="Group_Id"</code> or <code>id=Group_Name</code>. Is there a way I can get the class applied?</p> <p><a href="http://f.cl.ly/items/0Y3R0W3Z193s3d1h3518/Capture.PNG" rel="nofollow noreferrer">http://f.cl.ly/items/0Y3R0W3Z193s3d1h3518/Capture.PNG</a></p> <h2>Update</h2> <p>I've tried implementing <code>IValidatableObject</code> on the Group view model instead of using a validation attribute but I still can't get the CSS class to apply:</p> <pre><code>public class Group : IValidatableObject { public int Id { get; set; } public string Name { get; set; } public IEnumerable&lt;ValidationResult&gt; Validate(ValidationContext validationContext) { if (string.IsNullOrEmpty(Name) &amp;&amp; Id == 0) { yield return new ValidationResult("Please create a new group or select an existing one.", new[] { "Group.Name" }); } } } </code></pre> <h2>Update 2</h2> <p>Self validation doesn't work. I think this is because the second parameter in the <code>ValidationResult</code> constructor isn't used in the MVC framework.</p> <p>From: <a href="http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2" rel="nofollow noreferrer">http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2</a></p> <blockquote> <p>In some situations, you might be tempted to use the second constructor overload of ValidationResult that takes in an IEnumerable of member names. For example, you may decide that you want to display the error message on both fields being compared, so you change the code to this:</p> <p><code>return new ValidationResult( FormatErrorMessage(validationContext.DisplayName), new[] { validationContext.MemberName, OtherProperty });</code></p> <p>If you run your code, you will find absolutely no difference. This is because although this overload is present and presumably used elsewhere in the .NET framework, the MVC framework completely ignores ValidationResult.MemberNames.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload