Is the section tag appropiate for grouping form elements?

拟墨画扇 提交于 2020-02-23 10:27:47

问题


I'm currently using p tags to surround form groupings (e.g. label, input and error message)...

p tags are what I've used for years but I'm having a bit of an issue as p tags cannot contains divs etc... so I'm considering using the 'section' tag to break up the form "sections" :)...

I'm look for some feedback if u guys think the use of this tag in this context is semantically/functionally appropriate... Forms for me as a web dev are 90% of my day so Really appreciate your thoughts and feedback :)

FYI (though inconsequential) I'm using Laravel Form for the examples!

Before

<p>
    <label>First Name</label>
    <input type="text" name="first_name" value="{{ Form::form_value('first_name', $user) }}">
    <span class="error_message">{{ $errors->first('first_name') }}</span>
</p>

<p>
    <label>Surame</label>
    <input type="text" name="surname" value="{{ Form::form_value('surname', $user) }}">
    <span class="error_message">{{ $errors->first('surname') }}</span>
</p>

After

<section>
    <label>First Name</label>
    <input type="text" name="first_name" value="{{ Form::form_value('first_name', $user) }}">
    <span class="error_message">{{ $errors->first('first_name') }}</span>
</section>

<section>
    <label>Surame</label>
    <input type="text" name="surname" value="{{ Form::form_value('surname', $user) }}">
    <span class="error_message">{{ $errors->first('surname') }}</span>
</section>

Thanks for your feedback


回答1:


The section element can be appropriate for grouping form elements, but certainly not for every input+label group. It’s a sectioning content element, which means that each section could be listed in the document’s outline. This is most likely not what you want.

A good case for section could be very long/complex forms, where each section possibly contains multiple fieldset elements. A rule of thumb for section use in general:

  • you should use section if you provide a heading (h1-h6) for the content;
  • you could use section if it would be appropriate to provide a heading for the content, but you don’t for some reason.

The p element is appropriate for grouping one or more input+label groups, as the second snippet in example 1 shows. As you want to include possible error messages, using a (meaningless) div element for the group, with p elements as children, seems to be appropriate:

<div>
  <p>
    <label>First Name</label>
    <input type="text" name="first_name" value="">
  </p>
  <p class="error_message"></p>
</div>



回答2:


I don't think <section> is the correct semantics to be using. You could use <fieldset>, but I think that's for multiple inputs or you could possibly surround everything in a <label>.

e.g.

<label>
  First Name
  <input type="text" name="surname" value="{{ Form::form_value('surname', $user) }}">
  <span class="error_message">{{ $errors->first('surname') }}</span>
</label>

Otherwise, a <p> tag is perfectly fine, there's not much semantics to a form anyway.



来源:https://stackoverflow.com/questions/14762717/is-the-section-tag-appropiate-for-grouping-form-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!