33

I have a very weird problem. When I'm submitting the form, it throws an error with server-side validation.

Here is my simple controller:

namespace App\Http\Controllers; use Newsletter; use Illuminate\Http\Request; class SubscriptionController extends Controller { public function subscribe(Request $request) { $request->validate([ 'email' => 'required|email', ]); } } 

Submitting the form gives me:

BadMethodCallException Method validate does not exist.

it should work according to:

https://laravel.com/docs/5.4/validation

2
  • 1
    The linked page does not have any mention of a $request->validate . it has multiple mentions of $this->validate($request,...) though Commented Aug 21, 2017 at 11:25
  • @apokryfos yes, I also found that later on, my bad. Commented Aug 21, 2017 at 11:34

9 Answers 9

46

In docs said:

$this->validate($request, [ 'email' => 'required|email', ]); 

This string - works :)

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

2 Comments

Works for Lumen too.
Works inside a Controller too.
10

You should try this:

$validateFields = array('email' => 'required|email'); $this->validate($request, $validateFields); 

OR

$this->validate($request, [ 'email' => 'required|email' ]); 

Comments

6

Well means its no longer available in 5.4 however its available in controller

Try:

 $this->validate($request, [ 'email' => 'required|email', ]); 

Comments

6

You can use the Validator service provider.

 namespace App\Http\Controllers; use Newsletter; use Illuminate\Http\Request; use Validator; class SubscriptionController extends Controller { public function subscribe(Request $request) { $request->validate($request->all(),[ 'email' => 'required|email', ]); } 

Comments

6
 $validator = \Validator::make($request->all(), [ 'mobile_number' => 'required']); if ($validator->fails()) { return redirect()->back() ->withErrors($validator) ->withInput(); } 

Hope this works for you..

Comments

4

Actually If you add the right controller, validate method should be already included. You can try adding below controller.

Instead: use App\Http\Controllers\Controller;

Comments

2

Add validator service and clear cache after making changes

namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Foundation\Validation\ValidatesRequests; class RecaptchaController extends Controller { public function store(Request $request) { $this->validate($request,[ 'name' => 'required', 'email' => 'required|email', 'phone' => 'required|min:10', 'g-recaptcha-response' => 'required|captcha' ]); return "success"; } } 

You can clear cache with the below artisan command on your terminal. Note: remember to check the directory you run the command.

php artisan cache:clear 

Comments

0
You can use regex pattern of this email validation. protected function validator(array $data) { $messages = array('email.regex' => 'Your email id is not valid.'); return Validator::make($data, [ 'email' => 'required|email|max:255|regex:/(.*)@myemail\.com/i|unique:users', ],$messages); } 

Comments

-3

let's add these two packages Best of luck

use Illuminate\Support\Facades\Validator; use Illuminate\Foundation\Auth\RegistersUsers;

1 Comment

Please refrain your will to post an untested answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.