Kyle Walter
Kyle Walter

Reputation: 29

How can I access Laravel 5.1 validation rules

How can I access all the methods that Laravel 5.1 provides us for validation. For example I have made custom request with artisan command php artisan make:request EventRequest in that file there is a public function rules(){ return[]; } in that function yo can specify html attributes names and the validation rules that you need. How can I access these validation rules(path to these rules). Please note that I don't want to make custom validation rules I have to access existing ones.

Upvotes: 0

Views: 665

Answers (2)

wunch
wunch

Reputation: 1092

I'm sure you're already aware of the documented list of available validation rules.

If you just want to access the code that is used to evaluate those rules: in Laravel 5.1, these built-in rule names are mapped to methods defined directly on the Validator class. (You can also check the API reference for that class)

For example, 'digits_between' will eventually use the validateDigitsBetween() method on that class. However, since those are protected methods, you can't call them directly yourself. You have to use Validator::make($request, $rules). See the docs on this.

(In Laravel 5.6, these methods are on a trait called ValidatesAttributes. So if for whatever reason you wanted to use them directly, you could just use that trait on your class.)

Upvotes: 1

Jesus Erwin Suarez
Jesus Erwin Suarez

Reputation: 1585

In your controller replace the Request with your validation namespace probably like this App\Http\Requests\EventRequest so it should look like this.

from

public function store(Request $requests)
{
   // code here
}

to

public function store(App\Http\Requests\EventRequest $requests) 
{
   // code here
}

or else you can use your validation namespace like so

use App\Http\Requests\EventRequest;

SomeControllerClass extends Controller { 
    public function store(EventRequest $requests) 
    {
       // code here
    }
} 

Hope that helps.

Upvotes: 0

Related Questions