Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does grails add a hidden checkbox to a form - and what does it do with it?

Tags:

input

grails

gsp

I have a checkbox on my GSP page as follows (which was stolen directly from the scaffolded "create" code for my domain object)...

<tr class="prop">
<td valign="top" class="name">
  <label for="isSelling"><g:message code="person.isSelling.label" default="Is Selling" /></label>
</td>
<td valign="top" class="value ${hasErrors(bean: personInstance, field: 'isSelling', 'errors')}">
    <g:checkBox name="isSelling" value="${personInstance?.isSelling}" />
</td>
</tr>

This works just fine, except when I look at the elements in the resulting form I have a hidden checkbox alongside the real one...

<tr class="prop">
<td valign="top" class="name">
  <label for="isSelling">Is Selling</label>
</td>
<td valign="top" class="value ">
    <input type="hidden" name="_isSelling" />
    <input type="checkbox" name="isSelling" id="isSelling"  />
</td>
</tr>

My questions are:

  1. why is it there?
  2. what does Grails do with it?
  3. if I am looking at the form values in Javascript, which input value should I take?

Just inspecting what happens when the checkbox gets set on and off in my page, it appears that the hidden one is ignored, so I am imagining there is some cunning processing going on when the submit action occurs which looks at the _isSelling and isSelling for some magical purpose. Anyone have any insight into what Grails is doing?

Thanks

like image 744
Simon Avatar asked Jan 20 '10 14:01

Simon


1 Answers

That's a spring thing. It adds that checkbox so that unchecked boxes are accountable. Some browsers won't push any information about an unchecked box so the hidden box is added to prevent binding errors.

like image 92
Brandon Avatar answered Nov 07 '22 14:11

Brandon