Note that there are some explanatory texts on larger screens.

plurals
  1. POOptional [Required] on complex types
    primarykey
    data
    text
    <p>I'm trying to get my viewmodel set up for a form that will collect information about people joining a team. The form must contain some leadup information, then a list of 5 "Team Members" (each containing a name, email, and phone), the first two of which will be required. For my validation I would like it to be on the individual fields, like this:</p> <pre><code>Person 1: Name: (validation messaage) Email: (validation message) Phone: (validation message) Person 2: Name: (validation messaage) Email: (validation message) Phone: (validation message) Person 3: Name: Email: Phone: Person 4: Name: Email: Phone: Person 5: Name: Email: Phone: </code></pre> <p>The relevant portion of my viewmodel is currently:</p> <pre class="lang-cs prettyprint-override"><code>[Required] public TeamMember TeamMember1 { get; set; } [Required] public TeamMember TeamMember2 { get; set; } public TeamMember TeamMember3 { get; set; } public TeamMember TeamMember4 { get; set; } public TeamMember TeamMember5 { get; set; } </code></pre> <p>so in my view, I just write:</p> <pre class="lang-cs prettyprint-override"><code>@Html.EditorFor(model=&gt;model.TeamMember1) @Html.EditorFor(model=&gt;model.TeamMember2) @Html.EditorFor(model=&gt;model.TeamMember3) @Html.EditorFor(model=&gt;model.TeamMember4) @Html.EditorFor(model=&gt;model.TeamMember5) </code></pre> <p>the editor template looks like this:</p> <pre class="lang-cs prettyprint-override"><code>@model MyProject.Models.TeamMember &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; 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;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Email) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Email) @Html.ValidationMessageFor(model =&gt; model.Email) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Phone) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Phone) @Html.ValidationMessageFor(model =&gt; model.Phone) &lt;/div&gt; </code></pre> <p>Sorry for throwing so much into one thread, but does anyone have a suggestion as to how best to set this up? I've thought about inheriting from RequiredAttribute and replacing [Required] on the TeamMember properties, but I'm not sure how to set the validation messages on the child fields. Right now, even if it's empty it passes the required check, I'm assuming because the objects are bound (and so not null) even though all of the properties are blank.</p> <p>Any feedback is appreciated.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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