Ant's
Ant's

Reputation: 13811

How to pass the user selected value from radioGroup to a action?

I have a radio button group like this :

<g:radioGroup name="myGroup" values="[1,2,3]" value="1" >
<p><g:message code="${it.label}" />: ${it.radio}</p>
</g:radioGroup>

Lets say that this in view named questions. User will select any answer from this and he will click a button named calculate. Which will redirect him to another action named calculate. Now in this action, how come I get the user selected answer from questions page?

Actually I will be having 70+ question on questions view page. How do I manage those? Any suggestions?

Thanks in advance.

Upvotes: 1

Views: 2493

Answers (1)

david
david

Reputation: 2587

Matching your example the answer will be in the myGroup parameter:

def calculate() {
  def answer = params.myGroup
}

I suggest renaming myGroup to a more meaningful name such as "question" plus its index e.g. question1, question2 etc. to differentiate between radio groups of questions. This enables you to iterate over all request parameters and assign answers accordingly:

def calculate() {
  def questions = ... //list of questions rendered previously
  questions.eachWithIndex { question, i ->
    question.answer = params["question$i"]
  }
}

Of course the question-answer assignment blocks depends on your specific logic/data model.

Upvotes: 1

Related Questions