CodeIgniter Forums
listErrors() always displays error - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: listErrors() always displays error (/thread-75759.html)



listErrors() always displays error - muuucho - 03-13-2020

I am trying to validate the news form from the docs tutorial and display errors in my view. However, I always get all the errors. 
My Controller:
Code:
public function create()
    {
        helper('form');
        $model = new NewsModel();
        if (! $this->validate([
            'title' => 'required|min_length[3]|max_length[255]',
            'body'  => 'required'
        ]))
        {
            if($_POST){
                return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
            }else{
                echo view('templates/header', ['title' => 'Create a news item']);
                echo view('news/create');
                echo view('templates/footer');
            }
        }
        else
        {
            $model->save([
                'title' => $this->request->getVar('title'),
                'slug'  => url_title($this->request->getVar('title')),
                'body'  => $this->request->getVar('body'),
            ]);
            echo view('news/success');
        }
    }
My view:
Code:
h2><?= esc($title); ?></h2>

<?= \Config\Services::validation()->listErrors(); ?>

<form action="/news/create" method="post">

    <label for="title">Title</label>
    <input type="input" name="title" value="<?= old('title')?>" /><br />

    <label for="body">Text</label>
    <textarea name="body"><?= old('body')?></textarea><br />

    <input type="submit" name="submit" value="Create news item" />

</form>
According to the docs listErrors() should contain only failed fields, but if I post the form with a title of let's say "title" and I leave the body empty, still both errors are displayed:
  • The title field is required.

  • The body field is required.


Also when I visit the form for the first time, both errors appear.


RE: listErrors() always displays error - muuucho - 04-13-2020

Lonnie uses a nice way of displaying errors in his Myth/Auth. It is implemented in the login form.


RE: listErrors() always displays error - InsiteFX - 04-14-2020

There are two ways of getting form errors.

PHP Code:
// All Errors
$this->validator->getErrors());

// Single Error
$this->validator->getError('fieldName')); 

Lonnie uses this in his views.

PHP Code:
<?= view('Myth\Auth\Views\_message_block'?>

This is his message_block code for the included view.

PHP Code:
<?php if (session()->has('message')) : ?>
    <div class="alert alert-success">
        <?= session('message'?>
    </div>
<?php endif ?>

<?php if (session()->has('error')) : ?>
    <div class="alert alert-danger">
        <?= session('error'?>
    </div>
<?php endif ?>

<?php if (session()->has('errors')) : ?>
    <ul class="alert alert-danger">
    <?php foreach (session('errors') as $error) : ?>
        <li><?= $error ?></li>
    <?php endforeach ?>
    </ul>
<?php endif ?>

Look at the Myth/Auth Controller to see how he is doing the Errors.


RE: listErrors() always displays error - muuucho - 04-14-2020

Indeed, but I have been looking for a way to display errors tied to its input field. I just wanted to say that my problem is solved, and the solution is to be found in Myth/auth:

Controller:
PHP Code:
$rules = [
            
'login'    => 'required',
            
'password' => 'required',
        ];
        
//.............

        
if (! $this->validate($rules))
        {
            return 
redirect()->back()->withInput()->with('errors'$this->validator->getErrors());
        }
//............ 
view:
PHP Code:
<div class="form-group">
                            <
label for="login"><?=lang('Auth.emailOrUsername')?></label>
                            <input type="text" class="form-control <?php if(session('errors.login')) : ?>is-invalid<?php endif ?>"
                                   name="login" value="<?= old('login');?>" placeholder="<?=lang('Auth.emailOrUsername')?>">
                            <div class="invalid-feedback">
                                <?= session('errors.login'?>
                            </div>
                        </div>