Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thc1967's avatar

Custom Validator Not Firing

I'm trying to use a custom validator on a user profile page that manages a password change. The validator doesn't seem to be getting called.

This is not the only custom validator in the solution. I have a few others and those are all called as I expect them to be.

If there's a better way to validate the current user's password on a password change, I'll be happy to hear about it. As it is, here's what I'm doing...

I created a custom validator class called ExtendedValidator.php. Into that, I've placed a few new functions. The one that isn't being called is coded as follows:

    public function validatePwdForUser($attribute, $value, $parameters)
    {
        $this->requireParameterCount(1, $parameters, 'pwd_for_user');

        $userId = array_shift($parameters);

        $where[] = [
            ['id', '=', $userId],
            ['password', '=', bcrypt($value)]
        ];

        dd($where);
        return (1 == Users::where($where)->count());
    }

When I put a dd($parpmeters) as the first line in that method, it never dies and dumps. This tells me that the method is never getting called.

I think my controller should call that method because in the controller, I have the following code:

    public function update(Request $request, Profile $profile)
    {
        $authUser = Auth::user();
        $hasPassword = (strlen($request->input('new_password')) > 0);

        $validations = [];

        if ($hasPassword) {
            $validations [] = [
                'password' => 'required|pwd_for_user:' . $authUser->id,
                'new_password' => 'required|confirmed|min:6|password_complexity:2,ipd,ultera,helpdesk'
            ];
        }

        $this->validate($request, $validations);

When I put a dd($validations) right before the $this->validate() call, it dumps the 2 validations I expect to see. The first one should call my custom validation method. Actually, the second one should call a different custom validation method and that doesn't seem to be firing, either.

In my AppServiceProvider.php file, I reference my custom validator class:

    public function boot()
    {
        Validator::resolver(
            function($translator, $data, $rules, $messages, $customAttributes) {
                return new ExtendedValidator($translator, $data, $rules, $messages, $customAttributes);
            }
        );

What have I done wrong?

Thanks!

0 likes
6 replies
jlrdw's avatar

Where are you calling validatePwdForUser from?

Snapey's avatar

I have never done this so, just referring to the docs, you don't register it the same? docs talk of Validate:extend ?

also, your validator will need to change since you cannot create the same bcrypt password twice, so your code will never find a match. You need to get the users password then use the check function instead.

thc1967's avatar

I'm not calling validatePwdForUser. The $this->validate() in the controller should do that for me based on the validation array I pass it.

I'd not terribly worried about the logic and bcrypt() calls right now. My main concern is why the validator doesn't seem to be firing.

As I mentioned, I have other controller classes that use other methods on the same validator class and they fire. I can't figure out why they're not firing for this one controller class, or perhaps for these two specific methods.

Snapey's avatar

are those other validators using camel / snake case?

For instance, if your method was validateHello then there is a good chance that your rule would be called 'hello'

but if it is validatePwdForUser, is it 'pwdForUser' or 'pwd_for_user' ??

As a start, I would just call it userpassword (all lowercase) and then rename your method validateUserpassword - just to rule this out.

thc1967's avatar

I think it's actually worse than that. $this->validate() in this particular controller class doesn't ever actually do anything, even if I strip the validations array back to only stock Laravel validations.

Completely different problem. :(

thc1967's avatar

And it was the way I was building the $validations array. Too deep. Bad PHP.

Please or to participate in this conversation.