skip to Main Content

I’m trying to send my validation errors to another method by using redirect

public function save()
    {

        //validasi input
        if (!$this->validate([
            'judul' => 'required|is_unique[komik.judul]'
        ])) {
            return redirect()->to('/komik/create')->withInput();
        }

this is the create() method

public function create()
    {
        session();
        $data = [
            'title' => 'Form Tambah Data Komik',
            'validation' => ConfigServices::validation()

        ];

        return view('komik/create', $data);
    }

This is a snippet of my create.php view form where I’m trying to validate

<form action="/komik/save" method="post" enctype="multipart/form-data">

                <?php d($validation) ?>
                <?= $validation->listErrors();; ?>

this is the form
enter image description here

The problem is that the validation errors in save() is not sent to the create() method. But the validation errors exist in the save() method which I can prove by adding
$validation = ConfigServices::validation(); dd($validation);
in save(). This is what happens when I click "Tambah Data" button after I add the code
enter image description here

as you can see there is a validation error, it’s just not sent to the create() method
enter image description here

I tried using return view(), this works but it creates another problem. I would like to use return redirect() instead.

This is my routes

$routes->get('/', 'Pages::index');
$routes->get('/komik/create', 'Komik::create');
$routes->get('/komik/edit/(:segment)', 'Komik::edit/$1');
$routes->post('/komik/save', 'Komik::save');
$routes->delete('/komik/(:num)', 'Komik::delete/$1');
$routes->get('/komik/(:any)', 'Komik::detail/$1');

What can I do to solve this problem? Thanks

2

Answers


  1. Chosen as BEST ANSWER

    I found the solution, apparently there's a bug with redirect()->withInput() in CI. You need to use validation_errors(), validation_list_errors() and validation_show_error() to display validation error. Also you don't need to write $validation = ConfigServices::validation();. Read https://codeigniter4.github.io/CodeIgniter4/installation/upgrade_430.html#redirect-withinput-and-validation-errors


  2. A. Use Flashdata instead.

    CodeIgniter supports “flashdata”, or session data that will only be
    available for the next request, and is then automatically cleared.

    I.e:

    save() method.

    public function save()
    {
        //validasi input
        if (!$this->validate([
            'judul' => 'required|is_unique[komik.judul]'
        ])) {
            $validation = ConfigServices::validation();
    
            return redirect()
                ->to('/komik/create')
                ->with("redirectedErrors", $validation->listErrors())
                ->with("redirectedInput", $this->request->getVar());
        }
    }
    

    with()

    Adds a key and message to the session as Flashdata.

    public with(string $key, array<string|int, mixed>|string $message) : $this

    B. Then, modify the create() method to pass on the flashed data to the View.

    create() method.

        public function create()
        {
            return view('komik/create', [
                'title' => 'Form Tambah Data Komik',
                'validation' => ConfigServices::validation(),
                'redirectedErrors' => session()->getFlashdata("redirectedErrors") ?: "",
                'redirectedInput' => session()->getFlashdata("redirectedInput") ?? [],
            ]);
        }
    

    C. Lastly, in your View file (create.php), you may access the passed-on data normally.

    create.php View file.

    <form action="/komik/save" method="post" enctype="multipart/form-data">
        <?php echo empty($validation->getErrors()) ? $redirectedErrors : $validation->listErrors() ?>
    
        <input id="first_name" name="first_name" type="text" value="<?php echo old('first_name', array_key_exists('first_name', $redirectedInput) ? $redirectedInput['first_name'] : '') ?>"/>
    
    </form>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search