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

Hello, I have question about shield for codeigniter 4. 
I have backend with form for edit and add new users. I use it like this:
PHP Code:
$this->validation->setRules([
    'username'        => 'is_unique[users.username,id,{user_id}]',
    'email'            => 'required|valid_email|unique_email[{user_id}]',
    'password'        => 'permit_empty|strong_password',
    'password_confirm' => 'matches[password]',
]);

if (!
$this->validation->withRequest($this->request)->run()) {
    return redirect()->back()->withInput()->with('errors'$this->validation->getErrors());
}
    

But when I submit a form, I expect to be redirected back to the form and show errors from validation. But I got CodeIgniter\Shield\Exceptions\ValidationException. 
I looked at sources and there is a trait for it - CheckQueryReturnTrait.
How to work with it, I just want to show validator errors like usual, but this throw exception and generate error.
Thank you.
Reply
#2

1. What class did throw the ValidationException?
2. What is the validation error message?
Reply
#3

Did you find a solution? because in every controller where I use validation it gives me the same error:

PHP Code:
CodeIgniter\Shield\Exceptions\ValidationException 
Reply
#4

(08-14-2022, 02:52 AM)chronic Wrote: Did you find a solution? because in every controller where I use validation it gives me the same error:

PHP Code:
CodeIgniter\Shield\Exceptions\ValidationException 

Hello, yes, this problem is what I have.

I have partial solution. Look at Config\Auth.php and set false the recordActiveDate variable. Then it start working. 
Nobody helped, maybe I overlooking something.
Reply
#5

Thanks janroz, disabling "recordActiveDate" actually works.

But I wonder if we are the only ones with this problem, or there is something wrong with this piece of code in vendor/codeigniter4/shield/src/Authentication/Authenticators/Session.php

PHP Code:
    /**
    * Updates the user's last active date.
    */
    public function recordActiveDate(): void
    
{
        if (!$this->user instanceof User)
        {
            throw new InvalidArgumentException(
                __METHOD__ '() requires logged in user before calling.'
            );
        }

        $this->user->last_active Time::now();

        $this->provider->save($this->user);
    

Exactly on this line:

PHP Code:
$this->provider->save($this->user); 
Reply
#6

ValidationException shows the reason in the exception message.
Why don't you fix it?
Reply
#7

(08-14-2022, 08:32 PM)kenjis Wrote: ValidationException shows the reason in the exception message.
Why don't you fix it?

Sorry kenjis, but for me it is not clear what is not working, otherwise I was not asking for help here.

But I have seen, how janroz suggested, that if I disable recordActiveDate in the Auth.php file everything will work again.
So I would like to understand if I'm doing something wrong, where is the error, because I certainly have limits in finding it.

I attach the link to the image of the error page, maybe you can help me.

https://postimg.cc/LhsGSxtp

Thank you
Reply
#8

I cannot read, but the error message is:
[Image: 2022-08-15-18-18-24.png]
Reply
#9

(This post was last modified: 08-15-2022, 02:57 AM by chronic.)

(08-15-2022, 02:20 AM)kenjis Wrote: I cannot read, but the error message is:
[Image: 2022-08-15-18-18-24.png]

The point is that if I disable recordActiveDate every error message is shown to me correctly at every input in the form, as in the image below:

[Image: screencapture2.png]

I am wrong something, and if so where can I look to understand what am I wrong? or could there be some problem with shield or with the latest update of Codeigniter 4.2.4, which I didn't have before despite recordActiveDate being true? Because to me after the update this problem occurs on all forms.
Reply
#10

What do you do in the page?
Can you show code?
Do you know why you call the recordActiveDate() method?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB