0

I have a FormRequest with the withValidator() method (which I copied exactly as it is in the Laravel documentation), but when executing the class an error is returned.

Class:

<?php

namespace App\Http\Requests\Conversation;

use Illuminate\Foundation\Http\FormRequest;

class StoreConversationRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        return [
            'type' => 'required|in:text,media',
            'text' => 'required_if:type,text|string',
            'medias' => 'required_if:type,media|array',
            'medias.*' => 'file|max:5120|mimes:jpg,jpeg,png,pdf,mp3,mp4',
            'phone' => 'required|numeric',
            'conversation' => 'required|exists:conversations,id',
        ];
    }
    
    /**
     * Configure the validator instance.
     *
     * @param  \Illuminate\Validation\Validator  $validator
     * @return void
     */
    public function withValidator($validator)
    {
        $validator->after(function ($validator) {
            if ($this->somethingElseIsInvalid()) {
                $validator->errors()->add('field', 'Something is wrong with this field!');
            }
        });
    }
}

Error:

Method App\Http\Requests\Conversation\StoreConversationRequest::somethingElseIsInvalid does not exist.

I tried replacing $this with $validator but the error persists:

Method Illuminate\Validation\Validator::somethingElseIsInvalid does not exist.
3
  • 1
    It's not obvious, but that's supposed to be a custom function, not a stock function. If you don't need to do any additional validation, just remove that function. Commented Jun 23, 2022 at 19:19
  • Certainly. I just added the code from the documentation to test, then this error came. So I haven't even started adding my code. Commented Jun 23, 2022 at 19:22
  • @Lucas somethingElseIsInvalid() is a test function that it expects you to write. It's not some magical feature of Laravel that detects other errors. Commented Jan 11, 2024 at 3:14

2 Answers 2

1

I solved problem with this:

/**
 * Configure the validator instance.
 *
 * @param  \Illuminate\Validation\Validator  $validator
 * @return void
 */
public function withValidator($validator)
{
    $validator->after(function ($validator) {
        if ($validator->errors()->isNotEmpty()) {
            $validator->errors()->add('field', 'Something is wrong with this field!');
        }
    });
}

But I don't understand why a code in the documentation is not working.

Sign up to request clarification or add additional context in comments.

Comments

0

I quite didn't get from where did you find this function:

somethingElseIsInvalid()

Try In this way:

   public function withValidator( $validator )
{

    if ( $validator->fails() ) {
        // Handle errors
    }
    ...
}

1 Comment

as I said, I copied the code from the documentation. And your code didn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.