Search code examples
c#asp.netasp.net-mvcasp.net-identity

Username and Email validation


I am using Entity Framework and trying to do a validation on my Email and Username fields on the edit user View. I know how to do the validation but I could not find where to do that.

Here is my View:

<div class="form-horizontal">
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.Id)

    <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Company, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.Company, Model.Companies.Select(company => new SelectListItem()
       {

           Text = company,
           Value = company,
           Selected = company == Model.Company
       }), new { @class = "form-control" })

            @Html.ValidationMessageFor(model => model.Company, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-10 pull-right">
            <button class="btn btn-default" type="reset" onclick="location.href='@Url.Action("Index", "ManageUsers")'"><span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span> @FMS.Resources.Global.back</button>
            <button class="btn btn-info" type="submit"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span> @FMS.Resources.Global.save</button>
        </div>
    </div>
</div>

For the Name and Company fields I easily did the validation because they both are on the ApplicationUser model (check below). The problem is that Email and Username are not there because they are inherited.

public class ApplicationUser : IdentityUser
{
    [Required(ErrorMessageResourceName = "field_cannot_be_empty", 
              ErrorMessageResourceType = typeof(FMS.Resources.Global))]
    public string Name { get; set; }

    public IEnumerable<string> Companies { get; set; }

    [Required(ErrorMessageResourceName = "field_cannot_be_empty", 
              ErrorMessageResourceType = typeof(FMS.Resources.Global))]
    public string Company{ get; set; }

If I try to define Email and username on the above model it says that it is already defined (Screenshot). The problem is that I cannot find where so I can put the validation there.


Solution

  • They are defined in the base class as virtual:

    public virtual string Email { get; set; }
    public virtual string UserName { get; set; }
    

    So you need to override this properties and add your annotation. Like this:

    [Required(ErrorMessageResourceName = "field_cannot_be_empty", 
              ErrorMessageResourceType = typeof(FMS.Resources.Global))]
    public override string Email { get; set; }