0

I'm using a custom request class for laravel form validations.

This is my request class

class ContactUsRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'lname' => 'required'
        ];
    }

   /**
   * Get the error messages for the defined validation rules.
   *
   * @return array
   */
    public function messages()
    {
        return [
            'lname.required' => 'please enter the last name'
        ];
    }

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

And this is where I call it,

public function send(ContactUsRequest $request) {
        $validator = $request->validated();

        if ($validator->fails()) {
            return redirect('/contactus')
                            ->withErrors($validator)
                            ->withInput();
        } else {
            ContactUs::create($request->all());

            return redirect('/contactus');
        }
    }

But when I input correct values I get this,

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR) Call to a member function fails() on array

2 Answers 2

1

That's because request object automatically do that for you and you don't need to redirect back manually and $validator variable contains validated inputs so in your case you don't need to do anything and you can remove if and redirect safely

public function send(ContactUsRequest $request) {
            ContactUs::create($request->validated());

            return redirect('/contactus');
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Using form request classes

If validation fails, a redirect response will be generated automatically to send the user back to their previous location. The errors will also be flashed to the session so they are available for display. If the request was an AJAX request, a HTTP response with a 422 status code will be returned to the user including a JSON representation of the validation errors.

In order to capture validation failure you may use the Validator facade

E.g

use Illuminate\Support\Facades\Validator;
//...

public function send(Request $request) {
        $validator = Validator::make($request->all(), [
            'lname' => 'required'
            // ...
        ]);

        if ($validator->fails()) {
            return redirect('/contactus')
                            ->withErrors($validator)
                            ->withInput();
        }

        ContactUs::create($request->all());

        return redirect('/contactus');
}

Form Request Validation Documentation

Manually Creating Validators Documentation

And we can keep ContactUsRequest like this.

public function send(ContactUsRequest $request) {
        $validator = $request->validated();

        ContactUs::create($request->all());

        return redirect('/contactus');
}

5 Comments

Thanks @foued-moussi. I added a little code and hope it's correct :)
Need to use ContactUsRequest class alsooo :)
in cas of injecting ContactUsRequest $requestin your send method the Validator::make()will be ignored in case of failure.
Think we doesn't need t since that part will be handled with custom request class. Can I add custom request class also to your code so I can accept ;)
I rejected your suggest edit by mistake, could you redo it ?

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.