7

I have a attribute called status in my domain which is String type can have any one of two values Applied , NotApplied

I have two check boxes to input this value. in my edit page i want to display these two check box.

If the value of status is Applied then the corresponding checkbox must be checked.

my code

 <g:message code="publicRuleInstance.course.label" default="Applied" />
<g:checkBox name="status " value="${publicRuleInstance?.status }" />

<g:message code="publicRuleInstance.course.label" default="NotApplied" />
<g:checkBox name="status " value="${publicRuleInstance?.status }" />

but here both the checkboxes are checked.

there must be a way to check the value i.e if the status = Applied then that perticular checkbox must be cheched else it should be unchecked.

Is there any way to doing it?

maaz
  • 3,534
  • 19
  • 61
  • 100

2 Answers2

5

Use the checked attribute to control the state of your checkBox as described in the docs. Here you could add any expression to determine the state of the g:checkBox:

<g:message code="publicRuleInstance.course.label" default="Applied" />
<g:checkBox name="status " value="Applied" checked="${publicRuleInstance?.status == 'Applied'}"/>

<g:message code="publicRuleInstance.course.label" default="NotApplied" />
<g:checkBox name="status " value="NotApplied" checked="${publicRuleInstance?.status == 'NotApplied'}"/>

If you just want to allow one of the values - Applied or NotApplied a g:radioGroup would be the better choice. With a checkBox the user could choose both values Applied and NotApplied.

aiolos
  • 4,637
  • 1
  • 23
  • 28
2

Value of checkBox should be boolean

<g:message code="publicRuleInstance.course.label" default="Applied" />
<g:checkBox name="status " value="${publicRuleInstance?.status =="Applied"}" />

<g:message code="publicRuleInstance.course.label" default="NotApplied" />
<g:checkBox name="status " value="${publicRuleInstance?.status == "NotApplied" }" />
Aram Arabyan
  • 2,339
  • 1
  • 17
  • 30
  • Is there any hard code rule that saying checkBox should have boolean value? – maaz Jul 26 '12 at 14:03
  • No - `g:checkBox` could have any `value`. – aiolos Jul 27 '12 at 18:28
  • 1
    It is logical that value of checkbox should be boolean. Other question is how groovy will parse value you pass. http://groovy.codehaus.org/Groovy+Truth – Aram Arabyan Aug 01 '12 at 09:09
  • 1
    checked attribute must be a boolean, but value attribute can be any value. See https://grails.github.io/grails-doc/3.0.x/ref/Tags/checkBox.html – peveuve Nov 05 '15 at 11:33