Radio Button Groups

Introduction

A radio button group is a set of related buttons. Selecting a button in such a set automatically deselects the other buttons.

The FormFiller component represents a single button by the same class that representing a check box: SetaPDF_FormFiller_Field_Button. The button set is represented by the SetaPDF_FormFiller_Field_ButtonGroup class. 

Setting and Getting a Value

The SetaPDF_FormFiller_Field_ButtonGroup class offers, as any other field implementation, the getValue() and setValue() methods:

getValue()

Get the export value of the current active button.

setValue()

Set the value / active button of the button group.

Another way to set a value is to "push" the desired button. The buttons of a button group can be accessed by the getButtons() method. The button can be activated intuitive: 

getButtons()

Get all buttons of the button group.

PHP
$buttons = $field->getButtons();

$randomIndex = array_rand($buttons);
$buttons[$randomIndex]->push();

Examples

PHP
// get the fields
$fields = $formFiller->getFields();
 
$radio1 = $fields->get('radio 1');
$radio1->setValue('A'); // checks the button with the export value "A"
 
$radio2 = $fields['radio 2'];
$buttons = $radio2->getButtons();
$radio2->setValue($buttons[2]); // Checks the 3rd available button
 
$radio3 = $fields->get('radio 3');
$buttons = $radio3->getButtons(); // returns 3 buttons
$buttons[1]->push(); // deselects button with the index 0 and 2
$buttons[2]->push(); // deselects button with the index 0 and 1
 
$radio4 = $fields->get('radio 4');
if (null !== $radio4->getDefaultButton()) {
    $radio4->setValue($radio4->getDefaultButton());
}