4
Vote

Html.EditorFor should set HTML5 required attribute for required values

description

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.

References:
http://www.w3.org/html/wg/drafts/html/master/forms.html#the-required-attribute
http://msdn.microsoft.com/en-us/library/ie/hh673544(v=vs.85).aspx#New_input_states_support
http://msdn.microsoft.com/en-us/library/ie/hh772943(v=vs.85).aspx

comments

jongalloway wrote Oct 24, 2013 at 8: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)
{
    base.AddAttributesToRender(writer);
    writer.AddAttribute("required","required",false);
    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 9:34 PM

Ignore my last comment. Dumbest comment of all time.

jongalloway wrote Oct 25, 2013 at 7:21 PM

Big picture, it'd be cool to pull in Dean Hume's MVC HTML5 pack: https://mvchtml5.codeplex.com/