Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Change order of generated input elements in asp-for checkbox helper #2994

Closed
rblackman opened this issue Aug 18, 2015 · 4 comments
Closed

Change order of generated input elements in asp-for checkbox helper #2994

rblackman opened this issue Aug 18, 2015 · 4 comments
Assignees
Milestone

Comments

@rblackman
Copy link

When rendering a checkbox input. There is a hidden input with a false value rendered after the checkbox input. See the example below.

<input asp-for="Foo" type="checkbox"/>
<label asp-for="Foo">Foo?</label>

becomes

<input data-val="true" data-val-required="The Foo field is required." id="Foo" name="Foo" type="checkbox" value="true" />
<input name="Foo" type="hidden" value="false" />
<label for="Foo">Foo?</label>

It is relatively common to style a label based on a checkboxes checked state e.g.

input[type="checkbox"] + label {
    /* style a */
}
input[type="checkbox"]:checked + label {
     /* style b */
}

The addition of the hidden input breaks this sort of selector.

I would suggest, putting the hidden input first (since it doesn't seem to matter which order the inputs are rendered in). So the above code would become:

<input name="Foo" type="hidden" value="false" />
<input data-val="true" data-val-required="The Foo field is required." id="Foo" name="Foo" type="checkbox" value="true" />
<label for="Foo">Foo?</label>
@danroth27
Copy link
Member

@dougbu @DamianEdwards

@Eilon
Copy link
Member

Eilon commented Sep 2, 2015

@Eilon
Copy link
Member

Eilon commented Sep 17, 2015

Solution:

  1. The Html.CheckBox HTML helpers and <input type="checkbox"> tag helper will render only the checkbox inline
  2. These helpers will not render the hidden input, and instead add it to the current form's context
  3. The form HTML helper will render all the extra hidden inputs at the bottom of the form
  4. A new "hidden" <form> tag helper (editorbrowsable=never) will capture all <form> elements regardless of attributes and its sole job is to render the extra hidden inputs at the bottom of the form
  5. If the HTML/tag helper cannot find a form context then it will fall back to the current behavior of rendering the hidden input immediately after the checkbox

@Eilon Eilon added this to the 6.0.0-beta8 milestone Sep 17, 2015
@Eilon Eilon modified the milestones: 6.0.0-beta8, 6.0.0-rc1 Sep 24, 2015
@danroth27 danroth27 assigned ajaybhargavb and unassigned dougbu Oct 8, 2015
ajaybhargavb added a commit that referenced this issue Oct 14, 2015
…form

- #2994
- Affects both HtmlHelper and TagHelper scenarios
- Checkboxes not enclosed in a form will generate inline hidden tags
- Added necessary properties to FormContext
- Added some functional and unit tests
@ajaybhargavb
Copy link
Contributor

d40e661

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants