Bringing pre-selected radio button option via Cakephp

Asked

Viewed 218 times

0

I got the following Form down below.

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

But, I want for example that the first option is preselected, some 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'));

?>

Is not duplicate of How to bring the radio button checked with a Bank result

  • 1

    It’s not enough to do 'value' => "D" or something like that?

  • Wow, I think it’s the tired kkk' is just that ! I was trying to put checked = 'D' so I wouldn’t, thank you Anderson, if you want to put the answer, I mark it as correct !!

1 answer

2


In accordance with the documentation, just set the value of the attribute value to configure which element will be selected when the form is rendered.

Follow the documentation excerpt:

'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, just set its value in 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'));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.