tags:

views:

497

answers:

1

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

+3  A: 

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.

Brandon
Binding errors when? Do you mean when the page is being rendered or on the POST/GET back to the server on Submit? If it is the submit then I thought that the HTML form definition ignored hidden elements, so I don't see how it helps.
Simon
I mean when the form is submitted.Hidden elements are not ignored - the most common use case for them is to maintain a stateless server when forms span multiple pages.The 'DISABLED' attribute will prevent elements from being submitted.http://htmlhelp.com/reference/html40/forms/input.html
Brandon