CodeIgniter Forums
Form Validation : Return Current View - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Form Validation : Return Current View (/showthread.php?tid=82166)



Form Validation : Return Current View - InstantT - 06-20-2022

Hello everyone,

I would like to know how to return the current view on which the user is, when validating a form.

Is it possible?

In my controller, i have : 
PHP Code:
if (!$this->validate([
       'keyword' => 'required|min_length[2]',
    ])) {
        return view('home', [
            'validation' => $this->validator,
        ]);
    

Instead of "home" I would like the view to be the one the user is on
I also included in my layout, a file "_errors-form.php" which contains :
PHP Code:
<?php use Config\Services?>

<?php $validation Services::validation(); ?>
<?php $errors 
$validation->getErrors(); ?>

<?php if(!empty($errors)): ?>
    <ul>
       <?php foreach ($errors as $error): ?>
           <li><?= esc($error?></li>
       <?php endforeach ?>
    </ul>
<?php endif; ?>


Is this the best method?

Thanks in advance for your help


RE: Form Validation : Return Current View - kenjis - 06-22-2022

(06-20-2022, 07:23 AM)InstantT Wrote: In my controller, i have : 
PHP Code:
if (!$this->validate([
       'keyword' => 'required|min_length[2]',
    ])) {
        return view('home', [
            'validation' => $this->validator,
        ]);
    

Instead of "home" I would like the view to be the one the user is on

Why don't you change the "home" to the view file name that you want?


RE: Form Validation : Return Current View - ikesela - 06-23-2022

better if return to where it from and show error (include input).

Code:
return redirect()->back()->withInput()->with('errors', service('validation')->getErrors());



RE: Form Validation : Return Current View - InstantT - 06-25-2022

Thanks for you reply.

(06-22-2022, 04:28 PM)kenjis Wrote: Why don't you change the "home" to the view file name that you want?

Because I wanted the error to be displayed on the current page where the user is.

But I will use your option, it's the easiest