We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Radio buttons and default values

Hello,

I've just filed this on GitHub, but I was wondering if I was wrong.

How do you use Phalcon\Forms\Element\Radio so that you can have default values working?

Okay, I've come up with a solution, hope it'll help. Inside form class:

$this->add(new Radio('published'));

And then in view:

{{ form.render('publish', ['value':'Y'])  }}
{{ form.render('publish', ['value':'N'])  }}
edited Oct '14

For default values, or values from request just use \Phalcon\Tag($tagId,$value) like this:

For set values in radio use this:

$radioElement = new Radio('published',array('value' => 'Y'));
$this->add($radioElement);

For default pre-choosed values:

\Phalcon\Tag::setDefault('published','Y'); // this will select Radio with value Y

From request or model you can do somethig like this:

if($this->request->getPost('published')){
   \Phalcon\Tag::setDefault('published',$this->request->getPost('published'); // and this will set the value which is selected
}

Then you can se more than one default values like this:


\Phalcon\Tag::setDefaults(array(
      'tagId'       => 'value',
      'tagId1'      => 'value1'
));

I hope this helps. :))

Otherwise, you have an error in names, in form you initializing "published" and in template you trying to access "publish" when you dont have created the "publish" element in form then will be not rendered.