'Set default radio button in Cakephp
The below code creates some radio buttons and html to go with them, it works well however I now want to set the first radio button as the default selected one and im not sure what needs to be added.
<?php
for ($i =0; $i < count($packages); $i++){
echo "<div class='package-outter'>";
echo "Name: ".$packages[$i]['Package']['name']."<br>";
echo "Number of Campaigns (per month): ".$packages[$i]['Package']['quantity']."<br>";
echo "Price: ".$packages[$i]['Package']['price']."<br>";
if ($i == 0){
echo $this->Form->input('package', array(
'type' => 'radio',
'options' => array($packages[$i]['Package']['id'] => $packages[$i]['Package']['name'],),
'class' => 'testClass',
));
}else{
echo $this->Form->input('package', array(
'type' => 'radio',
'options' => array($packages[$i]['Package']['id'] => $packages[$i]['Package']['name'],),
'class' => 'testClass',
'hiddenField' => false, // added for non-first elements
));
}
echo "</div>";
}
?>
Solution 1:[1]
If you try the associated value option it should work.
<?php
echo $this->Form->input('status', array(
'div' => true,
'label' => true,
'type' => 'radio',
'legend' => false,
'required' => false,
'hiddenField'=>false,
'options' => array(
1 => 'High',
2 => 'Medium',
3 => 'Low'
)
'value' => 'Medium'
) ); ?>
Solution 2:[2]
<?php
echo $this->Form->input('status', array(
'div' => true,
'label' => true,
'type' => 'radio',
'legend' => false,
'required' => false,
'hiddenField'=>false,
'options' => $status,
'value' => 1 => default value of input
));
Solution 3:[3]
Try this
$this->Form->control('company_id', [
'label' => 'Escolha a empresa',
'options' => $companies ,
'value'=> key($companies),
'type' => 'radio',
'required' => true
])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alexandre Strapacao G. Vianna |
| Solution 2 | |
| Solution 3 | Filippi Rizzi |
