Welcome Guest, Not a member yet? Register   Sign In
CI4 session_write_close() when redirect
#1

I am using CI4 an just learning and never using CI before.

I got this error:

Code:
PHP Fatal error:  Uncaught Exception: Serialization of 'Closure' is not allowed in [no active file]:0
Stack trace:
#0 [internal function]: session_write_close()
#1 {main}
  thrown in [no active file] on line 0

Here my Controller:

PHP Code:
public function create()
    {

        $data = [
            'title' => 'Add new banner',
            'validation' => \Config\Services::validation()
        ];
        return view('admin/banners/add'$data);
    }

    public function save()
    {
        if (!$this->validate([
            'banner_name' => 'required',
            'banner_caption' => 'required',
            'banner_details' => 'required',
            'image' => 'required'
        ])) {
            $validation = \Config\Services::validation(); // dumb here all data show
            return redirect()->to('/banners/create')->withInput()->with('validation'$validation); // dumb in view cannot show data
        }

        $this->BannersModel->save(
            [
                'banner_name' => $this->request->getVar('banner_name'),
                'banner_caption' => $this->request->getVar('banner_caption'),
                'banner_details' => $this->request->getVar('banner_details'),
                'image' => $this->request->getVar('image'),
                'publish' => 1
            
]
        );

        session()->setFlashdata('message''Data Saved');

        return redirect()->to('/banners');
    


I already try dumb $Validation before
PHP Code:
dd$validation = \Config\Services::validation()); 

when dumb show all the value.
But after redirect there are no value error in validation and i got that error.

what that i do wrong?


best regards,

Capah Maga
Reply
#2

(This post was last modified: 06-18-2021, 04:59 AM by includebeer.)

PHP Code:
->with('validation'$validation

You can't save $validation in the session like that. It's an instance of the validation library, that's why it crashes in session_write_close().

Here is an example to send the error messages to the redirected page. Use validate() to validate the rules, then use validator to get the error messages.
You can see a detailed example in this tutorial: https://includebeer.com/en/blog/how-to-b...r-4-part-6

PHP Code:
if ( ! $this->validate($rules))
{
    return redirect()->back()->withInput()->with('errors'$this->validator->getErrors());

CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB