mercredi 17 août 2016

laravel validation 5.1 with authenticated user

Im trying to do some validation on my registration form in my validator method. I'm concatenating $user->id to my name & email array keys so users cant register with the same name & email address thats already taken.

my AuthController below

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    protected function validator(array $data)
    {

        return Validator::make($data, [
            'name' => 'required|http://regex:/^[a-zA-Z]+$/u|max:255|unique:users,name,'.$user->id,
            'email' => 'required|email|max:255|unique:users'.$user->id,
            'password' => 'required|confirmed|min:6',
        ]);
    }

}

every time i hit submit i get a Undefined variable: user error message so i try to add $user = Auth::user(); above my validator method like this

    // retrieve authenticated user
    $user = Auth::user();
    protected function validator(array $data)
    {

        return Validator::make($data, [
            'name' => 'required|http://regex:/^[a-zA-Z]+$/u|max:255|unique:users,name,'.$user->id,
            'email' => 'required|email|max:255|unique:users'.$user->id,
            'password' => 'required|confirmed|min:6',
        ]);
    }

but i get a syntax error, unexpected '$user' error message. im not sure what im overlooking, thanks for all the help



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire