Welcome Guest, Not a member yet? Register   Sign In
Form Validation : Return Current View
#1
Question 
(This post was last modified: 06-21-2022, 12:36 PM by InstantT. Edit Reason: Delete HTML Div )

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
Reply
#2

(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?
Reply
#3

(This post was last modified: 06-23-2022, 01:13 AM by ikesela.)

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

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

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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB