Bringing pre-selected radio button via CakePhp

0

I have the following Form below.

If I pass checked=checked it brings the last selected option.

But, for example, I want the first option to be pre-selected, any hint / suggestion?

<?php 

echo "<label> Selecione o tipo de entrada</label>";
echo $this->Form->input('tipo', array(
     'type'   => 'radio',
     'before' => '<div ' . $style . '>',
     'separator' => '</div><div ' . $style . '>',
     'options' => array(
           'D' => $this->Html->image(
               "botoes/dinheiro.png", array(
                    "alt" => "Dinheiro", 
                    "title" => "Dinheiro", 
                    "style" => "height: 32px")),
           'P' => $this->Html->image(
               "botoes/porcento.png", array(
                    "alt" => "Porcentagem", 
                    "title" => "Porcentagem", 
                    "style" => "width: 28px")),
           'H' => $this->Html->image(
               "botoes/hora.png", array(
                    "alt" => "Hora", 
                    "title" => "Hora", 
                    "style" => "height: 32px"))),
     'id' => 'tipo', 
     'label' => 'Dinheiro/Porcento', 
     'style' => '', 
     'legend' => false, 
     'onclick' => "formular()", 
     'checked' => 'checked',
     'required'));

?>
  

Not a duplicate of How bring the radio button checked with a Bank Result

    
asked by anonymous 06.06.2017 / 20:05

1 answer

2

According to the documentation / a>, just set the value of the value attribute to configure which element will be selected when the form is rendered.

Here is the excerpt from the documentation:

  

'value' - Sets or selects the value of the affected element (s)

     

For radio buttons or select pickers it defines which element will be selected when the form is rendered (in this case 'value' must be assigned a valid, existent element value).

If you want the first element to be selected, simply set its value to value :

echo $this->Form->input('tipo', array(
     'type'   => 'radio',
     'before' => '<div ' . $style . '>',
     'separator' => '</div><div ' . $style . '>',
     'options' => array(
           'D' => $this->Html->image(
               "botoes/dinheiro.png", array(
                    "alt" => "Dinheiro", 
                    "title" => "Dinheiro", 
                    "style" => "height: 32px")),
           'P' => $this->Html->image(
               "botoes/porcento.png", array(
                    "alt" => "Porcentagem", 
                    "title" => "Porcentagem", 
                    "style" => "width: 28px")),
           'H' => $this->Html->image(
               "botoes/hora.png", array(
                    "alt" => "Hora", 
                    "title" => "Hora", 
                    "style" => "height: 32px"))),
     'id' => 'tipo', 
     'label' => 'Dinheiro/Porcento', 
     'style' => '', 
     'legend' => false, 
     'onclick' => "formular()", 
     'value' => 'D',                  // <===
     'required'));
    
06.06.2017 / 20:30