Welcome Guest, Not a member yet? Register   Sign In
Form value repopulate
#1

(This post was last modified: 05-24-2022, 11:58 PM by vinyl.)

For a form I am working on I am trying to repopulate the fields after a submit and validation detected issues.
No matter what I try, it does not seem to work. 
My controller looks like this:

Code:
public function index()
    {
        helper(['form', 'url']);
        if (!$this->validate([

            'naam' => 'required'
         
        ])) {         
            echo view('form', [
                'validation' => $this->validator,
            ]);
        } else {
            echo "success!";
        }
    }

The view looks like this:
Code:
$attributes = ['class' => 'needs-validation', 'id' => 'form'];
echo form_open('public', $attributes);

      <div class="mb-3">
        <div class="row">
          <div class="col-12 col-md-6">
            <label for="Naam" class="form-label">Je naam</label>
            <input type="text" id="naam" name="naam" class="form-control <?php if ($validation->hasError('naam')) { echo "is-invalid"; } ?>" value="<?= set_value('naam') ?>">
          </div>
        </div>
      </div> 

If I look at the examples in de docs this should be sufficient to repopulate a form but it stays blank. Any ideas?

In the manual for CI 4 there is no mention of the repopulate option anymore under the validation library but I see the options are now listed under the forms section. Even in the examples there is no mention of this option, nor is it under the upgrade page (Upgrade Validations — CodeIgniter 4.1.9 documentation). Is this ommited or does it need to be implemented in a different way? I would be noteworthy to mention this on a upgrade page.
Reply
#2

(05-24-2022, 11:49 PM)vinyl Wrote: For a form I am working on I am trying to repopulate the fields after a submit and validation detected issues.
No matter what I try, it does not seem to work. 
My controller looks like this:

Code:
public function index()
    {
        helper(['form', 'url']);
        if (!$this->validate([

            'naam' => 'required'
         
        ])) {         
            echo view('form', [
                'validation' => $this->validator,
            ]);
        } else {
            echo "success!";
        }
    }

The view looks like this:
Code:
$attributes = ['class' => 'needs-validation', 'id' => 'form'];
echo form_open('public', $attributes);

      <div class="mb-3">
        <div class="row">
          <div class="col-12 col-md-6">
            <label for="Naam" class="form-label">Je naam</label>
            <input type="text" id="naam" name="naam" class="form-control <?php if ($validation->hasError('naam')) { echo "is-invalid"; } ?>" value="<?= set_value('naam') ?>">
          </div>
        </div>
      </div> 

If I look at the examples in de docs this should be sufficient to repopulate a form but it stays blank. Any ideas?

In the manual for CI 4 there is no mention of the repopulate option anymore under the validation library but I see the options are now listed under the forms section. Even in the examples there is no mention of this option, nor is it under the upgrade page (Upgrade Validations — CodeIgniter 4.1.9 documentation). Is this ommited or does it need to be implemented in a different way? I would be noteworthy to mention this on a upgrade page.

have you tried 
PHP Code:
value="<?= old('email') ?>" give it a shot and lemme know 
Reply
#3

to use this [ value="<?= old('email') ?>" ] , you need return like this [ withInput() ]:

return redirect()->back()->withInput()->
Reply
#4

CodeIgniter 4 User Guide - Global Functions and Constants - old
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

Why do the form helper documentation or those of validation not mention this?

So instead of using:

Code:
echo view('form', [
                'validation' => $this->validator,
            ]);

I need to do a redirect? Coming from CI3 this would have never crossed my mind, especially since the docs don't mention it anymore.

Thanks all for the help.
Reply
#6

Your code works as expected.

PHP Code:
<?php

namespace App\Controllers;

class 
Home extends BaseController
{
    public function index()
    {
        helper(['form''url']);

        if (! $this->validate([
            'naam' => 'required|min_length[100]',
        ])) {
            echo view('form', [
                'validation' => $this->validator,
            ]);
        } else {
            echo 'success!';
        }
    }


PHP Code:
<?php
$attributes 
= ['class' => 'needs-validation''id' => 'form'];
echo 
form_open('/'$attributes);
?>

<div class="mb-3">
    <div class="row">
        <div class="col-12 col-md-6">
            <label for="Naam" class="form-label">Je naam</label>
            <input type="text" id="naam" name="naam" class="form-control <?php
            
if ($validation->hasError('naam')) {
                echo 
'is-invalid';
            } 
?>" value="<?= set_value('naam'?>">
        </div>
    </div>
</div>

<?php
echo form_submit('''Send');
echo 
form_close();
?>
Reply
#7

Well, I have exactly what you posted and my form is not returning values.

About this:
Code:
return redirect()->back()->withInput()->

The documentation says:
Quote: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.

But set_value and old are both not working.
Reply
#8

Im writing because I had the same problem.
Solution for me:
1. In controller
Code:
function __construct()
    {
        helper('form');
    }
2. In view

Code:
value="<?php echo set_value('[your field]'); ?>"
Reply




Theme © iAndrew 2016 - Forum software by © MyBB