0

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 ?

6
  • I got the email to display "[email protected]" Commented Jun 12, 2015 at 18:47
  • I think the concatenation of the id could be the issue. Why do you do this? Commented Jun 12, 2015 at 18:55
  • Same error. This is very wired. When I take off required , it works. But that will be disaster. Commented Jun 12, 2015 at 18:55
  • I did that concatenation because I want to ignore the unique of email while editing. Commented Jun 12, 2015 at 18:57
  • You call the function the wrong way. First is the data you want to check against, second the rules. Commented Jun 12, 2015 at 19:03

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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'
    ]
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.