30

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

Parth Vora
  • 4,073
  • 7
  • 36
  • 59

9 Answers9

45

In docs said:

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

This string - works :)

linktoahref
  • 7,812
  • 3
  • 29
  • 51
arku
  • 1,687
  • 17
  • 16
8

You should try this:

$validateFields = array('email' => 'required|email');

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

OR

$this->validate($request, [
    'email' => 'required|email'
]);
halfer
  • 19,824
  • 17
  • 99
  • 186
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
5

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

Try:

 $this->validate($request, [
    'email' => 'required|email',
 ]);
Emeka Mbah
  • 16,745
  • 10
  • 77
  • 96
5
        $validator = \Validator::make($request->all(), [
            'mobile_number' => 'required',]);

        if ($validator->fails()) {
            return redirect()->back()
            ->withErrors($validator)
            ->withInput();
        }

Hope this works for you..

3

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',
                  ]);
           }
3

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;

ferdousulhaque
  • 179
  • 2
  • 10
1

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
Sanath L S
  • 1,309
  • 8
  • 10
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);
}
Switi
  • 359
  • 3
  • 6
-3

let's add these two packages Best of luck

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

Asad Ullah
  • 1
  • 1
  • 2