Welcome Guest, Not a member yet? Register   Sign In
Shield validation question
#11

(08-15-2022, 03:47 AM)kenjis Wrote: What do you do in the page?
Can you show code?
Do you know why you call the recordActiveDate() method?

Here a part of the code:

PHP Code:
    public function itemCreatePost()
    {
        $validationRules['parent'] = ['label' => 'Padre''rules' => 'required|is_natural''errors' => ['is_natural' => 'Il valore specificato non è valido.']];

        foreach ($this->data['languages'] as $language)
        {
            $validationRules['name_' $language["slug"]] = ['label' => 'Nome''rules' => 'required|string|max_length[255]'];
            $validationRules['url_' $language["slug"]] = ['label' => 'Url''rules' => 'required|string|max_length[255]'];
        }

        if (!$this->validate($validationRules))
        {
            return redirect()->back()->withInput();
        }
        else
        {
            $data = [
                'menu_id' => $this->data['request']->getPost('menu_id'),
                'parent_id' => $this->data['request']->getPost('parent') != $this->data['request']->getPost('parent') : '0',
                'order_id' => $this->menuItemsModel->getNewOrder($this->data['request']->getPost('menu_id'), $this->data['request']->getPost('parent')),
                'icon' => $this->data['request']->getPost('icon') != '' $this->data['request']->getPost('icon') : '',
                'created_at' => date("Y-m-d H:i:s"),
                'created_by' => user_id()
            ];

            $item_id $this->menuItemsModel->insert($data);

            if ($item_id)
            {
                foreach ($this->data['languages'] as $lang)
                {
                    $data_translations[] = [
                        'item_id' => $item_id,
                        'language' => $lang['slug'],
                        'name' => $this->data['request']->getPost('name_' $lang['slug']),
                        'url' => $this->data['request']->getPost('url_' $lang['slug'])
                    ];
                }

                $this->menuItemsModel->insert_item_translations($data_translations);

                session()->setFlashdata('message', ['type' => 'alert-success''content' => 'La voce del menu è stata creata con successo.']);

                addToLog(user_id(), 'Creata voce menu <b>' $this->data['request']->getPost('name') . '</b>');

                return redirect()->to(session()->get('referer'));
            }
            else
            {
                session()->setFlashdata('message', ['type' => 'alert-danger''content' => 'Si è verificato un errore nell\'inserimento nel database.']);

                return redirect()->back()->withInput();
            }
        }
    

I don't call it directly, but it is invoked by shield, and the reason is explained in the configuration file.

PHP Code:
  
 
/**
    * --------------------------------------------------------------------
    * Record Last Active Date
    * --------------------------------------------------------------------
    * If true, will always update the `last_active` datetime for the
    * logged in user on every page request.
    */
    public bool $recordActiveDate true
Reply
#12

(This post was last modified: 08-15-2022, 03:29 PM by kenjis.)

Thank you.

I got the cause.

redirect()->withInput() saves validation errors in the Session.
And the check of the validation errors in Model (checkQueryReturn()),
$this->validation->getErrors() returns the errors in the Session.

I sent a PR https://github.com/codeigniter4/shield/pull/383
Reply
#13

(08-15-2022, 02:57 PM)kenjis Wrote: Thank you.

I got the cause.

redirect()->withInput() saves validation errors in the Session.
And the check of the validation errors in Model (checkQueryReturn()),
$this->validation->getErrors() returns the errors in the Session.

I sent a PR https://github.com/codeigniter4/shield/pull/383

Thanks to you for your patience, but I would never be able to solve it on my own.

Now I know I just have to wait for a fix.

Thanks again.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB