-1

I have a view which has two fields. The required field validation works for one and does not work for the other. Am i missing something?

Not working:

<div class="form-group">
                    <div class="controls">
                        <label class="control-label" for="Selected Organizations">Selected Organization(s)</label>
                        <div class="panel panel-default">
                            <div class="panel-body" style="overflow-y: auto; max-height: 100px; min-height:50px" id="divSelOrgs">
                                @for (int i = 0; Model.Organizations != null && i < Model.Organizations.Count(); i++)
                                {
                                    var org = Model.Organizations[i];
                                    var checkedAttr = (org.Id != Guid.Empty) ? "checked='checked'" : "";
                                    <div class="row">
                                        <input type="hidden" name="Organizations.Index" value="@i" />
                                        <input type="hidden" name="Organizations[@i].Name" value="@org.Name" />
                                        <input type="hidden" name="Organizations[@i].Data" value="@org.Data" />
                                        <div class="col-xs-1">
                                            <input type="checkbox" name="Organizations[@i].Id" value="@org.Id" @checkedAttr data-org-selected="true" />
                                        </div>
                                        <div class="col-xs-11" data-toggle="tooltip" data-placement="top" title="@org.Data">@org.Name</div>
                                    </div>
                                }
                            </div>
                        </div>
                        @Html.ValidationMessageFor(m => m.Organizations)
                    </div>
                </div>

Working :

<div class="form-group">
                @Html.LabelFor(m => m.FirstName, new { @class = "control-label" })
                @Html.TextBoxFor(m => m.FirstName, new { @class = "form-control", placeholder = "First name" })
                @Html.ValidationMessageFor(m => m.FirstName)
</div>

Model Code:

 [Required]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }
 [Required(ErrorMessage = "The {0} field is required.")]
    [Display(Name = "Selected Organization(s)")]
    public List<OrganizationDTO> Organizations { get; set; }
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
TheFallenOne
  • 1,598
  • 2
  • 23
  • 57
  • Your question is unclear. Do you mean that the `RequiredList` validation is not working? If so, `RequiredList` is not a builtin MVC component. You'll need to check with the source of that attribute to ensure you're using it properly and that it actually functions correctly in the first place. – Chris Pratt Mar 24 '17 at 16:08
  • @ChrisPratt I changed it to Required. And the validation still does not work. – TheFallenOne Mar 24 '17 at 16:34
  • 2
    Required doesn't work on list types. Essentially, all you're saying is that the list property itself must exist, not necessarily that it has 1 or more items, and it always will exist, even if it's empty. – Chris Pratt Mar 24 '17 at 16:36
  • Not related, but generate your form controls correctly using `@Html.HiddenFor(m => m.Organizations[i].Name)` etc. And your checkbox makes no sense (if its checked, the `Id` property will be bound, and if not, the `Id` property will be its default value) –  Mar 24 '17 at 21:40

1 Answers1

1

dataannotation does not work on list types but you can validate a list with custom attribute for validation like

public class RequiredListAttribute : ValidationAttribute
{
    public override bool IsValid(object list)
    {
        var list = list as IList;
        if (list != null)
        {
            return list.Count > 0;
        }
        return false;
    }
}

and your property will be

[RequiredList(ErrorMessage = "The field is required.")]
public List<OrganizationDTO> Organizations  { get; set; }
Usman
  • 4,615
  • 2
  • 17
  • 33