Radio Button Validation


#1

I’m writing a quiz application and I need to validate the radio button
reponses. I do not have the questions in a database, I just have forms
which contain “question1” and then I strip out the “question” portion
and just insert the number as the question’s id. So now I’m faced with
the dilemma of making sure the radio buttons are pressed.

Radio buttons do not show up in params if a button is not pressed, so I
don’t even get the 0 or 1 of check boxes. Is there an additional field
I can add the radio_button tag so that it reports something like
“not_checked” for which I can raise an error?


#2

Xavier L. wrote:

Can’t you check for params[:radio_button_name].nil? in the controller?


#3

Hi Xavier ~

One option is to set a default value, but probably not ideal. The next
option would be some JavaScript that executes prior to the form
submission.

~ Ben


#4

Alex Y. wrote:

Xavier L. wrote:

Can’t you check for params[:radio_button_name].nil? in the controller?

I was afraid that would be my only option. It feels like a real hack.
When I have a quiz with upwards of a 60 questions, that’ll become a
pain.

Thanks for the help,
Xavier L.


#5

Xavier L. wrote:

Alex Y. wrote:

Xavier L. wrote:

Can’t you check for params[:radio_button_name].nil? in the controller?

I was afraid that would be my only option. It feels like a real hack.
When I have a quiz with upwards of a 60 questions, that’ll become a
pain.
The alternative would be to do what the checkbox_field tag does, which
is to hide a default field with the same name in the form so that at
least something comes through. You’d need to write your own helper to
do that, of course, which may or may not be what you’re after…


#6

you can create your own helper for that and the resulting view will not
be
any more complex than now

still, I struggle to understand how you could not at least use some file
for
questions even if you don’t have a database and simply loop through them