Reputation: 61
I am trying to perform validation without passing the $request variable, would someone be able to provide what I pass through the validator function? My code is below
public function change($id)
{
$user = User::find($id);
$user->name = request("name");
$user->date_of_birth = request("date");
$user->email = request("email");
$user->phone = request("phone");
$user->address = request("address");
$user->city = request("city");
$user->postcode = request("postcode");
$validator = Validator::make(request(), [
'name' => 'required',
'email' => 'email|string|max:255|unique:users',
'phone' => 'required|numeric',
'address' => 'required|numeric',
'postcode' => 'required|numeric',
'city' => 'required'
]);
if ($validator->fails()) {
return redirect("/user/edit")
->withErrors($validator)
->withInput();
}
$user->save();
return redirect('/user');
}
Again, i am trying this without $request
Any help would be appreciated!
Upvotes: 0
Views: 1483
Reputation: 1945
If you can't use $request, please use request helper which can be used as Request::all();
or request->all();
Upvotes: 0
Reputation: 14298
You can do by passing an array, so all()
returns all the fields passed, try using it like this:
$validator = Validator::make(request()->all(), ...
Another tip is to first make the validation then find the user and set its fields. The validation can be a guard in this case. You can also create a custom Form request and set the validation rules there.
Upvotes: 2
Reputation: 3040
If you don't want to use the $request
you can just use the request()
helper instead. For example:
$user = User::find($id);
$user->name = request("name");
$user->date_of_birth = request("date");
$user->email = request("email");
$user->phone = request("phone");
$user->address = request("address");
$user->city = request("city");
$user->postcode = request("postcode");
request()->validate([
'name' => 'required',
'email' => 'email|string|max:255|unique:users',
'phone' => 'required|numeric',
'address' => 'required|numeric',
'postcode' => 'required|numeric',
'city' => 'required'
])
$user->save();
return redirect('/user');
That works fine unless you want to specifically handle failed validation in a different way.
Upvotes: 0
Reputation: 18577
You can use facade to fetch that data and pass it to validator,
$input = Request::all();
Here is the documentation.
Upvotes: 0