Laravel请求,更新验证

时间:2017-06-30 15:43:34

标签: laravel laravel-5

我有一个表单会更新用户详细信息,但是出于显而易见的原因,如果用户想要更新名称而不是更新电子邮件地址,它将保持不变。然而,尽管我付出了所有的努力,但我无法绕过它。

所以我想说我的电子邮件是:

test@test.com

我正在更新我的个人资料

电子邮件仍然是test@test.com

它引发了一个错误,说电子邮件必须是唯一的。

所以我试过这个:

<?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',
    ];
    }
}

然而我得到了:

  

未定义的变量: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);
    }

2 个答案:

答案 0 :(得分:0)

这是短暂的

table { font-size: 0.9vw; }

在视图页面上,只使用旧数据填充所有输入。 Examlpe

public function update(UserRequest $request, $id)
{
    $user = User::findOrFail($id); 

    $user->name = $request->input('name');
    $user->email = $request->input('email');
    $user->twitter_personal = $request->input('twitterp');
    $user->facebook_personal = $request->input('facebookp');
    $user->instagram_personal = $request->input('instagramp');
    $user->telephone = $request->input('telephone');
    $user->save();

   return redirect()->back()->withInput()->with('message', $message);
}

答案 1 :(得分:0)

这个解决方案可以在两种情况下改变。

第一种情况如果经过身份验证的用户本身正在尝试更改电子邮件,则可以通过这种方式完成此操作。

'email' => 'required|email|unique:users,email,' . auth()->user()->id,`

第二种情况可能是SuperAdmin负责更改此案例的其他用户电子邮件,可以这样完成。

您的route文件。这里,通配符{user}是您需要传递的参数,用于标识哪些用户需要更新。

Route::get('/profile/user/{user}/edit', 'UsersController@edit');
Route::get('/profile/user/{user}', 'UsersController@update');`

validation中,您可以获取请求参数。

'email' => 'required|email|unique:users,email,' . request()->route()->parameter('user'),`

注意:参数键必须是通配符名称。在这种情况下{user}。在您的情况下,可能会有{id}{username}