Search code examples
radio-buttonphalcon

How to implement radio button in Phalcon\Forms\Element


I might be wrong, but I'm sure that it is the way how Radios have been implemented. There is no possible way to, say, have radio buttons for 'published' and 'not published' which would have their values set by default, since you'd have only $published in your model, and Phalcon does not search for values by name attribute, e.g. If you have:

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

It is rendering the last radio button only. This is not a behaviour you would expect for a radio! Obviously we can't name both radios 'published' since Phalcon wouldn't know to which one are we refering to, and still Phalcon will use those names to try and get default values.

Please suggest If you have anyone have solution regard this.


Solution

  • I am facing the same problem and I am sure that everyone facing the same proble.it seems it is not usable at all. I hope someone will prove me wrong, but i just can't figure how to make a group of two dependent Radio buttons.

    // will render only NO radio, because it is the last element of name "published"?

    use Phalcon\Forms\Element\Radio;
    
    $yes = new Radio("published", array("id" => "myYes", "value" => 1));
    $yes->setLabel("YES!");
    
    $no = new Radio("published", array("id" => "myNo", "value" => 2));
    $no->setLabel("NO!");
    

    So, please, how can we render more than one radio button? And of course we need this Phalcon\Forms\Element\Radio, because we want to attach validatrions, filters..

    Here is the solution I came with:

            $attr = array(
                'name' => 'published'
            );
            $yes = new \Phalcon\Forms\Element\Radio("Yes", $attr);
            $no = new \Phalcon\Forms\Element\Radio("No", $attr);
    
            $this->add($yes);
            $this->add($no);
    

    Hope this solution will work.