Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 Element 'legend' occurs too few times

I have the following View in MVC and I get the warning message: Validation (HTML5): Element 'legend' occurs too few times

@model Berwin.Models.ViewModels.UserViewModel

@{
ViewBag.Title = "Press";
}

<h2>Press Area</h2>

@using (Html.BeginForm("Register", "PressController", FormMethod.Post))
{
<fieldset>
    @Html.TextBoxFor(model => model.FullName)
</fieldset>

<fieldset>
    @Html.TextBoxFor(model => model.Company)
</fieldset>

<fieldset>
    @Html.TextBoxFor(model => model.EmailAddress)
</fieldset>

<fieldset>
    @Html.CheckBoxFor(model => model.JoinMailingList)
</fieldset>
}

Would like to know why I am getting this warning and what I need to do to fix this.

like image 503
ediblecode Avatar asked Mar 07 '12 14:03

ediblecode


3 Answers

To prevent Visual Studio incorrectly warning you that the "Element 'legend' occurs too few times" you need to correct Visual Studio's HTML and XHTML validation files.

VS will then treat the <legend> tag as optional inside a <fieldset> tag (as per the spec).

To do so, there are two files you need to edit: html_5.xsd and xhtml_5.xsd. For VS2010 these are found in (e.g.):

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Packages\schemas\html\

Steps to take:

  1. Close Visual Studio

  2. Using a text editor open the file html_5.xsd found in the folder above.

  3. Locate the following line:

    (note there are two lines which are similar, the first is correct, the second needs editing):

    <xsd:element name="legend" type="legendType" minOccurs="1" maxOccurs="1" />

  4. Edit the line to read:

    <xsd:element name="legend" type="legendType" minOccurs="0" maxOccurs="1" />

  5. Save the file

  6. Repeat the process for the file xhtml_5.xsd which is in the same folder

Now start Visual Studio and view your HTML5 file - the warning will be gone.

I hope that helps others, and I hope the .xsd files will be corrected in a future update.

UPDATE:

The line you need to find may be:

<xsd:element ref="legend" minOccurs="1" maxOccurs="1" vs:firstchild="true"/>

If so, change the minOccurs="1" attribute to minOccurs="0"

like image 97
Chris Avatar answered Oct 17 '22 01:10

Chris


According the HTML 5 spec, the <legend> tag is not a required element within a <fieldset>.

The legend element represents a caption for the rest of the contents of the legend element's parent fieldset element, if any.

Docs: http://www.w3.org/TR/html5/forms.html#the-legend-element

In your case, its just a warning provided by Visual Studio or a plugin. Its not required and there may be a way to supress the warning under Tools - Options - Text Editor - HTML - Validation. Here you can also switch the target of your validation (HTML 5, XHTML 1, etc)

like image 34
shanabus Avatar answered Oct 17 '22 00:10

shanabus


Legend is optional in a Fieldset.

But try this, to get rid of the warning:

    <fieldset>
        <legend>User</legend>
        @Html.TextBoxFor(model => model.FullName)
    </fieldset>
like image 11
Verkion Avatar answered Oct 17 '22 00:10

Verkion