Search code examples
phpcodeigniter-4

Undefined variable when error validation in CodeIgniter 4


I would like to check a form and display any errors. I can't understand how Code Igniter 4 allows me to check for the presence of the $validation variable in order to display an alert.

When I display the form with the create() function, I get an error message telling me that the variable does not exist, which is normal.

My problem is the following, how to check the presence of error in the view to display the alert or not?

My Controller

<?php namespace App\Controllers;

use CodeIgniter\Model\Site_model;

class Site extends BaseController
{
        public function __construct()
        {
                helper('html', 'url');
        }
        public function create()
        {
                helper(['form']);
                echo view('createSite');
        }

        public function store()
        {
                helper(['form']);
                $validation =  \Config\Services::validation();
                $validation->setRules([
                                'siteName' => 'required',
                                'password' => 'required',
                                'passwordConfirmation' => 'required|matches[password]'
                        ],
                        [   // Errors
                                'siteName' => [
                                        'required' => 'Merci de fournir un nom de site'
                                ],
                                'password' => [
                                        'required' => 'Merci de fournir un mot de passe'
                                ],
                                'passwordConfirmation' => [
                                        'required' => 'Merci de confirmer le mot de passe',
                                        'matches' => 'Les mots de passe ne correspondent pas.'
                                ]
                        ]
                );
                $validation->withRequest($this->request)
                        ->run();
                if (! $this->validate([]))
                {
                        echo view('createSite', [
                                'validation' => $validation
                        ]);
                }
                else
                {
                        echo view('Success');
                }
        }
}

My view

...
<main role="main" class="container vertical-center">
<div class="col">
<div class="alert alert-danger" role="alert">
<?= $validation->listErrors(); ?>
</div>
<?= form_open('site/store') ?>
          <div class="form-group">
            <label for="siteName" class="text-white">Nom du site</label>
            <input type="text" class="form-control" id="siteName" name="siteName" value="" placeholder="Bordeaux" required>
            <div class="invalid-feedback">
              Merci de renseigner un nom de site.
            </div>
          </div>
          <div class="form-group">
            <label for="password">Mot de passe d'administration</label>
            <input type="password" class="form-control" id="password" name="password" required>
            <div class="invalid-feedback">
              Merci de renseigner un mot de passe.
            </div>
          </div>
          <div class="form-group">
            <label for="passwordConfirmation">Confirmation du mot de passe d'administration</label>
            <input type="password" class="form-control" id="passwordConfirmation" name="passwordConfirmation" required>
            <div class="invalid-feedback">
              Merci de confirmer le mot de passe.
            </div>
          </div>
          <button class="btn btn-block btn-success" type="submit" name="submit-form">Envoyer</button>
</form>
</div>
...

I use <?= $validation->listErrors(); ?> to display errors, but at the first display of the form, before validating it, there are no errors so I get Undefined variable: validation


Solution

  • I got around the problem by checking to see if the $validation variable was initialized. It's still strange that CI doesn't check that the error array is empty before displaying errors (which doesn't exist in my case).

    The code in my View:

    <?php if(isset($validation)) { ?>
    <div class="alert alert-danger" role="alert">
      <?= $validation->listErrors(); ?>
    <?php } ?>