cphalcon
cphalcon copied to clipboard
[BUG]: Form bind not usable with Radio element
Describe the bug Based on how bind works and checks for elements, radios do not work (https://github.com/phalcon/cphalcon/blob/v4.1.3/phalcon/Forms/Form.zep#L160) This bug could affect multiple things like Tag::setDefault() for example.
To Reproduce The problem with radios is, that they need to be added with a different name, else they overwrite themself.
$entity = new \Vokuro\Models\Users();
$form = new \Vokuro\Forms\UsersForm($entity);
$form->remove('banned');
$form->add(new Radio('r0', ['name' => 'banned', 'value' => 'no']);
$form->add(new Radio('r1', ['name' => 'banned', 'value' => 'yes']);
// Simulate POST data
$form->bind(['banned' => 'yes'], $entity);
echo $entity->banned; // displays null or default value
Expected behavior The form should bind the value to the correct element.
Workaround The current workaround for this problem is the following. Maybe the workaround should be done automatically and not rendered ever.
$entity = new \Vokuro\Models\Users();
$form = new \Vokuro\Forms\UsersForm($entity);
$form->remove('banned');
$form->add(new Radio('r0', ['name' => 'banned', 'value' => 'no']);
$form->add(new Radio('r1', ['name' => 'banned', 'value' => 'yes']);
// Workaround (can apply filters here etc. (should not be rendered)
$form->add(new Hidden('banned'));
// Simulate POST data
$form->bind(['banned' => 'yes'], $entity);
echo $entity->banned; // displays yes now
Details
- Phalcon version: 4.1.3
- PHP Version: 7.4
- Operating System: debian
- Installation type: Compiling from source
- Zephir version (if any): none
- Server: Nginx
- Other related info (Database, table schema): none
I think the Problem also exists with Checkbox
Another issue regarding this bug: The radio which matches the entity value will never automatically be checked
Another bug I just found. Form bind doesn't work for array elements too:
$employee = new Select(
'employees[]',
Users::getForDropdown(),
[
'class' => 'form-control',
'required' => 'required',
'multiple' => 'multiple',
'size' => 5,
]
);
$employee->setDefault($entity->employees);
Then in Form::bind https://github.com/phalcon/cphalcon/blob/4.2.x/phalcon/Forms/Form.zep#L203
Obviously there is no key with the name employees[]
The solution for something like this, would be a new option entityKey
which you could set to employees
in this case. Then in the corresponding line, it should check if this option is set and use this key instead. This would also solve the radio problem from above.