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

Phalcon 5

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)
Can you try setting the
checkedwith the same value asvalue?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;
`
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
Resolved in https://github.com/phalcon/cphalcon/pull/16358
Thank you again @dugwood
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
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:
Worked out of the box when
$entity->status = '1'In Phalcon 5 the checked attribute is not automatically set anymore.
This is my current workaround:
@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:
into this:
because this value has no other meaning than to make “checked” worked. I thought maybe this could work:
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. .