codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.4k stars 1.9k forks source link

Docs: unclear usage of the `old()` global function #7677

Closed sammyskills closed 1 year ago

sammyskills commented 1 year ago

PHP Version

7.4

CodeIgniter4 Version

4.3.6

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

apache

Database

No response

What happened?

From the docs about the global old() function, there is this statement:

If you are using the form helper, this feature is built-in. You only need to use this function when not using the form helper.

If I understand correctly, the statement above means that if a developer is using the form helper, for example: form_open(), they do not need to use the old() function, as it is built-in.

Now I have a form, and I redirected back to the previous page using return redirect()->back()->withInput(), but the old values of the form are not retained when I don't use <?= old() ?> in the form value attribute.

Is this a mistake or is there a specific way to do this?

Steps to Reproduce

  1. Create a view, add some form element, for example:
    
    <?= form_open() ?>

<?= form_close() ?>



2. In your controller, just return back to the previous page, like so: `return redirect()->back()->withInput();`

### Expected Output

It is either the documentation is misleading or incomplete.

### Anything else?

_No response_
kenjis commented 1 year ago

The form helper means set_*() helper functions. https://codeigniter.com/user_guide/helpers/form_helper.html#set_value See https://github.com/kenjis/ci4-validation-tutorial/blob/main/app/Views/form2.php

At least, you don't use form helper at all in the following code:

<input type="text" name="name" value="">
<input type="email" name="email" value="">
sammyskills commented 1 year ago

Good catch, @kenjis .

I think it will be nice to have this line added to the docs:

The form helper means set_*() helper functions

WDYT?

kenjis commented 1 year ago

Okay, I sent #7678