As you know Laravel 5 changes the way you call the validator
, the old way is calling the validator facade
, but now there is the ValidatesRequests
trait in base Controller class, but the validate
method accepts the request as the values array, but when you define your route parameters, these values are not stored in Request
, so how can I validate those parameters ?
Edit:
Route:
Route::get('/react-api/{username}', 'ProfileController@getUsername');
Controller:
public function getUsername(Request $request, $username)
{
$v = $this->validate($request, ['username' => 'required']);
}
So, the question how can i validate this username parameter ?
Supposue the following route:
You can still validate
id
parameter as L4 way:If you need to validate concrete data, take a look the following example:
L5 let you do in two other ways. The first one, using a generic
Request
class injected in the controller:Finally, as second option, you can use Form request validation. Remember, every GET and POST value can be accessed via Request class
Manix's answer wasn't working for me, I was having the same issues as Iliyass. The issue is route parameters aren't automatically available to the FormRequest. I ended up overriding the all() function in my particular FormRequest Class:
Then you can code rules as normal: