cphalcon: [BUG]: Attribute 'checked' in Check element

Describe the bug

In Phalcon 5.0.x Check field clears ‘checked’ attribute on render.

To Reproduce

$checkField = new \Phalcon\Forms\Element\Check('checkField');
$checkField->setAttribute('checked', 'checked');
var_dump($checkField->getAttributes());
echo $checkField;

Expected behavior

It should render html with attribute ‘checked’. It works in phalcon 4.x, 3.x

Screenshots

Phalcon 4

image

Phalcon 5

image

Details

  • Phalcon version: 5.0.0beta3
  • PHP Version: 7.4.28
  • Operating System: Debian
  • Installation type: pecl
  • Server: Apache

Additional context

I tried this on attribute ‘disabled’ and it was working, so the bug is not about setting any attribute

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 19 (9 by maintainers)

Most upvoted comments

Can you try setting the checked with the same value as value?

$checkField = new \Phalcon\Forms\Element\Check('checkField');
$checkField
    ->setAttribute('value', 'one-two-three')
    ->setAttribute('checked', 'one-two-three')
;

echo $checkField;

It works in that configuration

What!

Is selected if the value property value and the checked property value are the same.

` /** * Processes the checked value */ private function processChecked() -> void { var checked, value; array attributes;

    let attributes = this->attributes;
    if !fetch checked, attributes["checked"] {
        let checked = null;
    }

    unset attributes["checked"];

    if checked !== null {
        if !fetch value, attributes["value"] {
            let value = null;
        }
        if checked === value { // Is selected if the value property value and the checked property value are the same. . <<<<======
            let attributes["checked"] = "checked"; 
        }
    }

    let this->attributes = attributes;
}

`

I did it as follows and it worked.

$input = new \Phalcon\Forms\Element\Radio('radio1', [ 'name' => 'radiogroup', 'value' => 'one-two-three' ]); $input->setDefault('one-two-three'); $input->setAttribute('checked', $input->getValue()); // <<<< Set the input value property value for the checked property.

I think the team was a little confused while writing

The main issue here is the test of checked value. It should use empty() for the test:

https://github.com/phalcon/cphalcon/blob/bf4e73cb50caa420ab0c38800e3a275fbc7b45c5/phalcon/Html/Helper/Input/Checkbox.zep#L123

As @amujib stated, testing 0 or (empty string) should be a valid option. It was working with Phalcon 4.

So instead of empty() there should only be a isset(). And we can assume that null is not a valid value.

Can you fix this? Or should I open another bug report @niden?

If you don’t mind please add an issue and reference this discussion/tag me.

Sorry guys, too busy at work to remember all these discussions 😕

I found another bug related to checkbox when using ‘0’ as unchecked value. To Reproduce

$checkField = new \Phalcon\Forms\Element\Check('checkField');
$checkField
    ->setAttribute('value', '1')
    ->setAttribute('unchecked', '0')
;

echo $checkField;

Expected Result <hidden name="checkField" value="0"><input type="checkbox" id="checkField" name="checkField" value="1" />

Actual Result 0<input type="checkbox" id="checkField" name="checkField" value="1" />

Yes, setAttribute() is definately not the solution. In Phalcon 3/4 the checked attribute was true of false when the Entities property value was the same as the default value attribute.

For example:

$status = new Check('status ', [
    'value' => '1',
]);
$status ->setLabel('Status');
$this->add($status );

Worked out of the box when $entity->status = '1'

$form->render('status');
<input type="checkbox" id="status" name="status" value="1" checked="checked">

In Phalcon 5 the checked attribute is not automatically set anymore.

<input type="checkbox" id="status" name="status" value="1">

This is my current workaround:

$status = new Check('status', [
    'value' => 1,
]);
if (!is_null($entity)) {
    $status->setAttribute('checked', $entity->status);
}
$status->setLabel('Status');
$this->add($status);

@niden I have a big project with many forms where i have checkboxes. Now in every checkbox that is only used for preview and that worked on v2, v3 and v4 (yes its old project mvp was building on v1) i have to modify code like this:

$checkField = new \Phalcon\Forms\Element\Check('checkField');
$checkField->setAttribute('checked', 'checked');

into this:

$checkField->setAttribute('value', 'workaround');
$checkField->setAttribute('checked', 'workaround');

because this value has no other meaning than to make “checked” worked. I thought maybe this could work:

$checkField->setAttribute('checked',$checkField->getAttribute('value'));

but it is not. I have to add value to checkboxes just to make it work. I can and probably do that but it`s workaround not a solution and potencialy a source of problems couse its easy to forget that $checkField->setAttribute(‘xyz’, ‘xyz’); will work and set attribute on field, but $checkField->setAttribute(‘checked’, ‘checked’) will not work and will not set attribute on the field becouse in ‘checked’ you have to add value before and set this value to checked. Sounds like a bug. .