getgrav / grav-plugin-form

Grav Form Plugin
http://getgrav.org
MIT License
53 stars 79 forks source link

Missing “for” attribute for label elements #560

Open nclm opened 2 years ago

nclm commented 2 years ago

It seems that Grab generates form labels without for attributes:

<div class="form-field  ">
    <div class="form-label">
        <label class="inline">Name <span class="required">*</span></label>
    </div>
    <div class="form-data" data-grav-field="text" data-grav-disabled="" data-grav-default="null">
        <div class="form-input-wrapper ">
            <input name="data[name]" value="" type="text" class=" " placeholder="Your name" autofocus="autofocus" required="required">
        </div>
    </div>
</div>

However, for attributes are very important to create the connection between the label and the form field. This should be rather look like this:

<div class="form-field  ">
    <div class="form-label">
        <label for='name' class="inline">Name <span class="required">*</span></label>
    </div>
    <div class="form-data" data-grav-field="text" data-grav-disabled="" data-grav-default="null">
        <div class="form-input-wrapper ">
            <input id='name' name="data[name]" value="" type="text" class=" " placeholder="Your name" autofocus="autofocus" required="required">
        </div>
    </div>
</div>

Could this be implemented? Thanks!

pamtbaau commented 2 years ago

You might want to take a look at the documentation about forms. Especially about the id attribute in section Common Fields Attributes.

id sets the field id as well as the for attribute on the label

hughbris commented 2 years ago

Because the value of labels is greatly reduced without id and setting id is not a requirement, my opinion is that I'd like to see a default id attribute set when it's not provided.

nclm commented 2 years ago

You might want to take a look at the documentation about forms. Especially about the id attribute in section Common Fields Attributes.

Oh thanks, I missed that!

Because the value of labels is greatly reduced without id and setting id is not a requirement, my opinion is that I'd like to see a default id attribute set when it's not provided

Yes, I agree as well. It could be randomly generated, generic like formname-field1, or even based on the name field (in which case the id attribute would be used to override the default). As long as it works. I cannot see a use case where someone would not want a for attribute on their labels, so it might as well be there by default.