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.
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:
Close Visual Studio
Using a text editor open the file html_5.xsd
found in the folder above.
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" />
Edit the line to read:
<xsd:element name="legend" type="legendType" minOccurs="0" maxOccurs="1" />
Save the file
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"
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)
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With