vendredi 30 juin 2017

Laravel request, update validation

I have a form which will update user details, however for obvious reasons if user want's to update a name but not a email address, it will stay the same. However despite my all efforts I cannot bypass that.

So let's say my email is:

test@test.com

and I am updating my profile

email is still test@test.com

it throws me an error saying that email must be unique.

So i tried this:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|alpha_spaces|min:5|max:30',
            'email' => 'required|email|unique:users,email,'.$user->id,
            'telephone' => 'required|numeric|max:15',
            'twitter' => 'alpha_dash',
            'facebook' => 'alpha_dash',
            'instagram' => 'alpha_dash',
        ];
    }
    public function messages()
    {
        return [
            'name.required' => 'Name is required',
            'name.alpha_spaces' => 'Name can only contain letters and spaces',
            'name.min' => 'Name must contain at least 5 characters',
            'name.max' => 'Name can only contain up to 30 characters',

            'email.required' => 'Email address is required',
            'email.email' => 'Email address must be a right format',
            'email.unique' => 'Email address must be unique',

            'telephone.required' => 'Telephone is required',
            'telephone.numeric' => 'Telephone can only contain numbers',
            'telephone.max' => 'Telephone can only contain maximum of 11 characters',

            'twitter.alpha_dash' => 'Twitter can only contain letters and dashes',

            'facebook.alpha_dash' => 'Facebook can only contain letters and dashes',

            'instagram.alpha_dash' => 'Instagram can only contain letters and dashes',
    ];
    }
}

however I get:

Undefined variable: user

   public function update(UserRequest $request, $id)
    {
        $user = User::find($id);
        $name = $request->input('name');
        $email = $request->input('email');
        $twitter = $request->input('twitterp');
        $facebook = $request->input('facebookp');
        $instagram = $request->input('instagramp');
        $telephone = $request->input('telephone');
        $user->name = $name;
        $user->email = $email;
        $user->twitter_personal = $twitter;
        $user->facebook_personal = $facebook;
        $user->instagram_personal = $instagram;
        $user->telephone = $telephone;
        $result = $user->save();
        if($result) {
        $message = 'success';
        }else{
        $message = 'error';
        }
       return redirect()->back()->withInput()->with('message', $message);
    }



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2t8S4k9
via IFTTT

Aucun commentaire:

Enregistrer un commentaire