how to set radio button for edit in grails

4.1k Views Asked by At

I am using grails 2.1.1 I have a domain where I want to save religion of member. I am saving it there is no problem. But when I go to edit page then it does not checked the original value.Suppose I am saving a religion for Buddhist and at the time of edit I want to be checked the value of it. But it is checking for Muslim. I want the field to be checked on edit page. Can anyone please help me on this please ??!!! Here are my attempts below ::

my domain class >>>

    class RadioButton {

    String religion

    static constraints = {
    }
}

my form page >>>

    <div class="fieldcontain ${hasErrors(bean: radioButtonInstance, field: 'religion', 'error')} ">
    <label for="religion">
        <g:message code="radioButton.religion.label" default="Religion"/>

    </label>
    <g:radio name="religion" value="muslim" checked="checked"/> Muslim <br/>
    <g:radio name="religion" value="hindu"/> Hindu<br/>
    <g:radio name="religion" value="christian"/> Christian<br/>
    <g:radio name="religion" value="buddhist"/> Buddhist
</div>
2

There are 2 best solutions below

1
On BEST ANSWER

You may want to consider the radioGroup tag within Grails instead of manually authoring your radio buttons.

However, if you decide to continue manually authoring your radio buttons you will need to account for selecting the current value. For example:

<g:radio name="religion" value="muslim" checked="${radioButtonInstance?.religion.equals('muslim')}"/>

In the above example you will notice that the checked attribute is being set to a boolean value (which is correct according to the documentation).

0
On

I think radioGroup is by far a better solution for you as you are using grails.

They main problem is that you are not passing the currently set religion to the GSP. There is nothing telling the radio group which religion has already been set by the user, instead Muslim has been hard-coded with the checked="checked".

Judging from your first line which sets the class of the div if the bean has an error, I assume you can access the currently set religion from the radioButtonInstance. Using the radioGroup tag you pass the currently set value as ${radioButtonInstance?.religion}, then we set the values and the labels you need, as shown:

<g:radioGroup name="religion"
              values="['Muslim', 'Hindu', 'Christian', 'Buddhist']"
              labels="['Muslim', 'Hindu', 'Christian', 'Buddhist']"
              value="${radioButtonInstance?.religion}">
    <p>${it.label}: ${it.radio}</p>
</g:radioGroup>

I would, however, suggest that you set the available religions as an enum class rather than hard coding it onto the GSP, as you might want to reuse it. You could then pass it as a variable in the model through the controller, calling it, for example, religions, then your radioGroup would look like:

<g:radioGroup name="religion"
              values="${religions}"
              labels="${religions}"
              value="${radioButtonInstance?.religion}">
    <p>${it.label}: ${it.radio}</p>
</g:radioGroup>