Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: The "setDefault" method does not work for forms of type "text" with name "value" #16382

Closed
hostbox2 opened this issue Jul 26, 2023 · 2 comments
Assignees
Labels
bug A bug report not a bug Reported issue is not a bug

Comments

@hostbox2
Copy link

The "setDefault" method does not work for forms of type "text" with name "value".

Example Form initialize:

class notificationsEditForm extends Form {

        public function initialize($entity = null) {
...
            $value = new Text('value', array(
                'maxlength' => '20'
            ));
            $value->setDefault('10');
            $value->addValidators(array(new Between(array(
                'minimum' => -999999999,
                'maximum' => 999999999,
                'message' => 'The field is filled incorrectly',
            ))));
            $this->add($value);
...
        }
    }

Example Form in controller:

$notificationsEditForm = new \dashboard\notificationsEditForm();

Example Form in view:

<?=$notificationsEditForm->render('value')?>

As a result, the form is displayed, but it has no default value (just a blank field).

IF you rename the form, for example, call the field "valuer" - the default value is inserted

Details

  • Phalcon 5.2.2
  • PHP Version: 8.1.20
  • Operating System: Ubuntu 22.04.2 LTS
  • Server: Nginx + Apache
@hostbox2 hostbox2 added bug A bug report status: unverified Unverified labels Jul 26, 2023
@niden niden self-assigned this Jul 30, 2023
@niden niden added not a bug Reported issue is not a bug and removed status: unverified Unverified labels Jul 30, 2023
@niden niden added this to Phalcon v5 Jul 30, 2023
@niden niden moved this to In Progress in Phalcon v5 Jul 30, 2023
@niden
Copy link
Member

niden commented Jul 30, 2023

This is not a bug, it is how the forms were built. You cannot have an element with the name "value".

This has been here since the v3 version maybe earlier (https://github.com/phalcon/cphalcon/blob/3.4.x/phalcon/forms/form.zep#L618C19-L618C19)

In order for the form to be able to get the values either from itself or the children elements, it needs to call some methods. In the case of elements getDefault() in the case of the form getValue().

The getValue() method is called in the form to get the value for the relevant element. In there, it checks if there are certain places that the value can be stored. One is the Form object and another the Element object. It checks the properties as well as getters. In your case you name your field value and the getValue() method tries to check the getter i.e. getValue() and therefore calls itself ending up in a crash.

Since it is a reserved word, you get null back.

@niden
Copy link
Member

niden commented Jul 30, 2023

I added a section in the docs

https://docs.phalcon.io/5.0/en/forms#reserved-names

Thank you for reporting this @hostbox2

@niden niden closed this as completed Jul 30, 2023
@niden niden moved this from In Progress to Implemented in Phalcon v5 Jul 30, 2023
@niden niden moved this from Implemented to Released in Phalcon v5 Aug 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report not a bug Reported issue is not a bug
Projects
Status: Released
Development

No branches or pull requests

2 participants