Re: QuickForm setChecked or setDefaults on Radios not in group?
| From: | Justin Patrin | Date: | Fri, 01 Oct 2004 20:19:13 +0000 |
| Subject: | Re: QuickForm setChecked or setDefaults on Radios not in group? | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-14722@lists.php.net to get a copy of this message | ||
On Fri, 01 Oct 2004 15:05:17 -0500, Sarah Gray <sarah@fabled.net> wrote:
> Hello,
>
> Is it possible to set radio defaults if the radio buttons are not part
> of a group?
>
> I tried using setChecked, as described here...
>
> http://pear.php.net/manual/en/package.html.html-quickform.html-quickform-radio.setchecked.php
>
> But this resulted in the first radio per group being checked, regardless
> of which radio I called it on.
>
> My examples are below, but if it is definitely not possible, please let
> me know.
> If it is possible, and you have a usage example or correction for me, I
> would be very
> appreciative. Thanks in advance.
>
> Sarah
>
> ****
>
> My examples.
>
> I.
> This would help me in two cases. On one form it is a layout issue
> When I make the radio buttons a group, by doing this, (in a loop)
>
> $radio[$i] =& HTML_QuickForm::createElement('radio', null,
> $alphabet[$i] .'.', '', $alphabet[$i]);
> $form->addGroup($radio, 'single_correct', '', ' ');
> $form->setDefaults(array('single_correct' => $selectedRadioValue));
>
> the functionality works, but they layout all together. I understand
> that I can use a separator, but in my case
> the radio buttons need to appear next to text input boxes so an
> administrator can create multiple choice options. As in
>
> A. (radio) Choice 1
> B. (radio) Choice 2
> C. (radio) Choice 3
>
> Where "Choice 1" etc are <input type="text">, followed by a <br
> />.
>
> When I put the radio buttons in a group, I get a layout like this, which
> isn't so helpful when you're creating a
> multiple choice question.
>
> Choice 1
> Choice 2
> Choice 3
> A. (radio) B. (radio) C. (radio)
>
> II.
> On another form it is not a layout issue, as the choice text is the
> radio text itself, but currently the radios are not in a group and I
> would prefer
> to keep it that way, to avoid re-writing.
>
> I tried using setChecked but as I said above, this resulted in the first
> radio per group being checked, regardless of which radio I called it on.
>
> This is my code. It is rendering a test and displaying the answers the
> user selected (drawing each choice in a loop).
>
> $form->addElement('radio', $question->question_id, $letter,
> $choice->choice_text, $choice->choice_id);
>
> if( $params['selectedChoices']) {
> if( in_array($choice->choice_id, $params['selectedChoices']) ){
> $sel =& $form->getElement($question->question_id);
> $sel->setChecked(true);
> }
> }
>
> ******************
>
> Tips, corrections, examples welcomed.
>
This seems to work just fine...
require_once('HTML/QuickForm.php');
$form = new HTML_QuickForm();
for ($i = 0; $i < 5; ++$i) {
$radio[$i] =& HTML_QuickForm::createElement('radio',
'single_correct', $i.'.', '', $i);
$form->addElement($radio[$i]);
}
$form->setDefaults(array('single_correct' => 3));
echo $form->toHtml();
--
paperCrane --Justin Patrin--