0

I have this model :

[Required(ErrorMessage = "Please provide a valid EmailAddress")]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required(ErrorMessage = "Please provide a company name")]
    [Display(Name = "Company")]
    public string CompanyName { get; set; }

    [Required(ErrorMessage = "Please provide a username")]
    [Display(Name = "Username")]
    public string UserName { get; set; }

    [Required(ErrorMessage = "Please select at least one language")]
    public int[] SelectedLanguages { get; set; }
[Required(ErrorMessage = "Please select at least one business unit")]
    public int[] SelectedBusinessUnits { get; set; }

Now when I do a post from my form using this model and I don't provide any of the values, I only get errormessages for Email, Company and UserName. I don't get messages for the SelectedLanguages or the SelectedBusinessUnits.

What am i doing wrong?

THis is the view

 @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
        {
            @Html.AntiForgeryToken()
            <h4>Create a new account.</h4>
            <hr />
            @Html.ValidationSummary("", new { @class = "text-danger" })

            <div class="form-group">
                @Html.LabelFor(m => m.CompanyName, new { @class = "col-md-2 control-label" })
                <div class="col-md-10">
                    @Html.TextBoxFor(m => m.CompanyName, new { @class = "form-control" })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
                <div class="col-md-10">
                    @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
                <div class="col-md-10">
                    @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
                </div>
            </div>

            <div class="form-group">
                @foreach (var la in Model.Languages)
                {
                    <input type="checkbox"
                           name="SelectedLanguages" value="@la.Id" id="@la.Id" />
                    <label for="@la">@la.Title</label>
                }
            </div>

            <div class="form-group">
                @foreach (var bu in Model.BusinessUnits)
                {
                    <input type="checkbox"
                           name="SelectedBusinessUnits" value="@bu.Id" id="@bu.Id" />
                    <label for="@bu.Id">@bu.Title</label>
                }
            </div>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" class="btn btn-default" value="Register" />
                </div>
            </div>
        }
Bart Schelkens
  • 1,235
  • 4
  • 21
  • 45
  • Works for me. Have you inspected `ModelState` for errors when you post? –  Feb 20 '15 at 08:39
  • I don't get in the Post-method for the registration. WHen i push the submit-button, I immediately get the validation-messages, but not for the two list I added. – Bart Schelkens Feb 20 '15 at 10:19
  • Do you mean your not getting client side validation errors for `SelectedLanguages` and `SelectedBusinessUnits` before you submt? (I assume they are listboxes) –  Feb 20 '15 at 10:22
  • @StephenMuecke that is correct. the clientside validation isn't triggered. – Bart Schelkens Feb 20 '15 at 10:23
  • Works for me. Can you show the view code associated with `SelectedLanguages` and `SelectedBusinessUnits` –  Feb 20 '15 at 10:29
  • Your problem is your generating a whole lot of checkboxes for those properties that all have values (you set them using `value="@la.Id"` etc) so of course its valid –  Feb 20 '15 at 10:37
  • But when I get in the method in my controller it says correctly that you should select SelectedBusinessUnits and SelectedLanguages. So then it works correctly. – Bart Schelkens Feb 20 '15 at 10:44
  • 1
    That probably because you have not checked any checkboxes. Unchecked checkboxes do not post back so the array will be null and it will be invalid on the server side (but not on the client side because they do have values on the client) –  Feb 20 '15 at 10:46
  • indeed, so there is no way to have that clientside validated? – Bart Schelkens Feb 20 '15 at 10:48
  • Not using checkboxes as you have. It would work if you used `@Html.ListBoxFor(m => m.SelectedLanguages, yourSelectList)` –  Feb 20 '15 at 11:31

1 Answers1

0

I think you have to go the way of writing a custom validation routine accompanied with a ValidationAttribute. Don't think a simple "out-of-the-box" validator exists for checking if one or more values are present in an array.

Check out this SO post to point you in the right direction.

Basic setup:

public class ArrayContainsValueAttribute: ValidationAttribute 
{
  // your checks here (pseudo)
  if(!array.Any())
    return false;

  return true;
}

[ArrayContainsValue(ErrorMessage = "Please select at least one business unit")]
public int[] SelectedBusinessUnits { get; set; }
Community
  • 1
  • 1
Youp Bernoulli
  • 5,303
  • 5
  • 39
  • 59