I use Laravel-Form-Request and i have two question:
1- as i create form request, to validate it from controller, i can't use
$request->validated()
and when i use it error said : Call to a member function validated() on null
and when i use
$request->validate()
there is no problem. But in Laravel Document said use validated()
. So where is the problem?
2- As i said i use validate()
and when everything(rules) is ok continue but when there is problem in matching rules just redirect to current page but i want to throw exception. how can i define that??
You use $request->validated
when you want to retrieve content of the request body which have been validated and not to validate the request. To perform the request validation It's always the validate
method
$rules = [];
$validator = Validator::make($request->all(), $rules);
if($validator->passes()){
// here you will get all validated data
$request_data = $request->validated();
}
If you want to throw an exception you have fails
method on Illuminate\Support\Facades\Validator
Facade so you want check if the validation fails and throw you exception like this
if($validator->fails())}{
throw new ThrowANewExceptionHere("Exception Message");
}