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

Phalcon\Forms\Element\Radio does not properly validate

Hi. I get errors

//messages for field type1
Type1 is required. 
Type1 InclusionIn.
//messages for field type2
Type2 is required
Type2 InclusionIn

Here is my code

$type1 = new Radio("type1");
$type1->setAttributes(array(
    'required' => true,
    'autocomplete' => 'off',
    'checked' => 'checked',
    'name' => 'type',
    'value' => 1
));
$type1->setFilters(array('int'));
$type1->addValidators(array(
    new PresenceOf(array(
        'message' => 'Type1 is required'
    )),
    new InclusionIn(array(
        'message' => 'Type1 InclusionIn',
        'domain' => array(
            1,2
        )
    )),
));
$this->add($type1);

$type2 = new Radio("type2");
$type2->setAttributes(array(
    'required' => true,
    'autocomplete' => 'off',
    'name' => 'type',
    'value' => 2
));
$type2->setFilters(array('int'));
$type2->addValidators(array(
    new PresenceOf(array(
        'message' => 'Type2 is required'
    )),
    new InclusionIn(array(
        'message' => 'Type2 InclusionIn',
        'domain' => array(
            1,2
        )
    )),
));
$this->add($type2);

I forgot the reason, something like each radio item was validated separately and not as the single item of data that it is. The answer is to create a radio group, a custom form element, I posted this some time ago: https://forum.phalcon.io/discussion/7471/radio-group

That was for my own website, so I didn't bother to improve upon it, sorry. It looks like a lot of code, but it's actually quite easy to understand.