This project is read-only.

Html.EditorFor should set HTML5 required attribute for required values


We should set the required attribute for required fields in addition to setting data-val attributes so we take advantage of HTML5 form validation features.

This would allow supporting HTML5 browsers (IE10 and up) to use browser HTML5 validation features when supported.



jongalloway wrote Oct 24, 2013 at 9:48 PM

Since this doesn't require an uplevel render check, I think this would take a one line code addition in RequiredFieldValidator - writer.AddAttribute("required","required",false);
// System.Web.UI.WebControls.RequiredFieldValidator
/// <summary>Adds the HTML attributes and styles that need to be rendered for the control to the specified <see cref="T:System.Web.UI.HtmlTextWriter" /> object.</summary>
/// <param name="writer">A <see cref="T:System.Web.UI.HtmlTextWriter" /> that represents the output stream to render HTML content on the client.</param>
protected override void AddAttributesToRender(HtmlTextWriter writer)
    if (base.RenderUplevel)
        string clientID = this.ClientID;
        HtmlTextWriter writer2 = (base.EnableLegacyRendering || base.IsUnobtrusive) ? writer : null;
        base.AddExpandoAttribute(writer2, clientID, "evaluationfunction", "RequiredFieldValidatorEvaluateIsValid", false);
        base.AddExpandoAttribute(writer2, clientID, "initialvalue", this.InitialValue);

jongalloway wrote Oct 24, 2013 at 10:34 PM

Ignore my last comment. Dumbest comment of all time.

jongalloway wrote Oct 25, 2013 at 8:21 PM

Big picture, it'd be cool to pull in Dean Hume's MVC HTML5 pack: