phalcon / cphalcon

High performance, full-stack PHP framework delivered as a C extension.
https://phalcon.io
BSD 3-Clause "New" or "Revised" License
10.79k stars 1.96k forks source link

[BUG]: Clear form does not work #15956

Closed bakos closed 2 years ago

bakos commented 2 years ago

Describe the bug

Clear is not working on form elements values. (https://docs.phalcon.io/5.0/pl-pl/api/phalcon_forms : Clears element to its default value)

To Reproduce Steps to reproduce the behavior:

$textField = new Text('textField', [
            'placeholder' => 'Text field to clear after submit',

        ]);
        $textField->clear();
        $this->add($textField);

This should clear element to its default value

Provide minimal script to reproduce the issue

Form:

class ExampleForm extends Form
{
    public function initialize($entity = null, $options = null)
    {
        $textField = new Text('textField', [
            'placeholder' => 'Text field to clear',
        ]);
        $textField->clear();
        $this->add($textField);
    }
}

Action in contoller

        $form = new ExampleForm();
        if ($this->request->isPost()) {
            if (!$form->isValid($_POST)) {
                $form->clear();
                $this->view->error = "form is cleared";
            }
        }
        $this->view->form = $form;

Template view

<form id="form" action="/index/index" method="post">
    This field should be cleared after submit
    <?php
    foreach ($form as $element)
    {
        echo $element;
    }
    ?>
</form>

Expected behavior

Fields values should be cleared. It works in phalcon 3.x and 4.x.

Details

Additional context

I found commit related to the problem:

https://github.com/phalcon/cphalcon/commit/9992bb24c0aaaf8518141b11dacd7433d1694cef

bakos commented 2 years ago

Ater few more tests if you change order and add field first and then you try to clear it then it couse Segmentation fault (11) - only in 5.x version. 3.x and 4.x order is not a problem

$textField = new Text('textField', [
     'placeholder' => 'Text field to clear after submit',
]);
$this->add($textField);
$textField->clear();
Deathamns commented 2 years ago

I think I'm facing the same problem. Win10, PHP 8.1.7, Phalcon v5.0.0RC2. The script below simply freezes (with at least one element and Form->clear()). A "windows problem reporting" process starts running at 30% CPU, which if I kill the script terminates as well.

<?php

class TestForm extends \Phalcon\Forms\Form {
    public function initialize() {
        $this->add(new \Phalcon\Forms\Element\Text('test'));
        // freezes with clear
        $this->clear();
    }
}

new TestForm;
alimo2 commented 2 years ago

When i use clear() on element, it goes to infinite loop!

Details: Phalcon version: 5.0.0RC3 PHP Version: 8.1.9

niden commented 2 years ago

Resolved in https://github.com/phalcon/cphalcon/pull/16191

Thank you @bakos @Deathamns @alimo2