Reputation: 543
I'm currently working on my Edit form in my Laravel application.
I request all the inputs from a form submit. I got :
array:6 [▼
"_method" => "PUT"
"_token" => "iWyonRYFjF15wK8fVXJiTkX09YSPmXukyGbBcHRA"
"phone" => "9786770863"
"email" => "[email protected]"
"address" => "23 School St Lowell MA 01851"
"$id" => "1"
]
My goal is to only validate only : phone, email, and address.
I've tried
$validator = Validator::make(
['phone' => 'max:20'],
['email' => 'required|email|unique:users,email,'. $id ],
['address' => 'max:255']
);
// dd(Input::get('email')); // HERE <------ I got the email to display
if ( $validator->fails()) {
return Redirect::to('user/profile/'. $id )->withErrors($validator)->withInput();
} else {
$user = User::findOrFail($id);
$user->phone = Input::get('phone');
$user->email = Input::get('email');
$user->address = Input::get('address');
$user->save();
It keep failing on me and say that
The email field is required.
But if I recall correctly the email field is there.
How can validate only a certain fields in php Laravel 5 ?
Upvotes: 0
Views: 6216
Reputation: 62278
Your Validator::make()
method call is a bit off.
When using this function, the first parameter is the array of data to validate (your request data), and the second parameter is your array of rules.
Your current code has you passing in three parameters. It is treating ['phone' => 'max:20']
as your data to validate, ['email' => 'required|email|unique:users,email,'. $id ],
as your rule, and then ['address' => 'max:255']
as your messages array.
It should be something like this:
$validator = Validator::make(
Input::all(),
[
'phone' => 'max:20',
'email' => 'required|email|unique:users,email,'. $id,
'address' => 'max:255'
]
);
Upvotes: 3
Reputation: 4855
It should be :
$validator = Validator::make($input, [
'phone' => 'max:20',
'email' => 'required|email|unique:users,email,'. $id ,
'address' => 'max:255']
);
It thinks you are passing the first line as the data to check, and the second line as the rules for your validation. It doesn't find an email key so it tells you it is required.
Upvotes: 3