Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 Validation

I use the following rules for validation on creating a new user:

protected $rules= [
    'name'    => 'required',
    'email'    => [
        'required',
        'unique:user',
        'email'
    ]
];

When updating an existing user I use the same ruleset as shown above but don't want a validation error if the user didn't change his email at all.

I currently resolve this by using the following:

if (!User::changed('email')) {
    unset($user->email);
}

It feels like a dirty workaround to me so I was wondering if there are better alternatives.

Also note that the changed method is something I wrote myself. Does anyone know if there is a native Laravel 4 method for checking whether a model property has changed?

Thanks!

like image 431
Kevin Op den Kamp Avatar asked May 13 '13 08:05

Kevin Op den Kamp


4 Answers

The unique validation rule allows to ignore a given ID, which in your case is the ID of the data set you are updating.

'email' => 'unique:users,email_address,10'

http://four.laravel.com/docs/validation#rule-unique

like image 91
Holger Weis Avatar answered Oct 30 '22 04:10

Holger Weis


One approach is to create a validation function in the model and call it with the controller passing in the input, scenario and id (to ignore).

public function validate($input, $scenario, $id = null)
{
    $rules = [];

    switch($scenario)
    {
        case 'store':
            $rules   = [
                'name'     => 'required|min:5|unique:users',
                'email'    => 'required|email|unique:users',
                'password' => 'required|min:4|confirmed'
            ];
            break;

        case 'update';
            $rules   = [
                'name'     => 'required|min:5|unique:users' .',name,' . $id,
                'email'    => 'required|email|unique:users' .',email,' . $id,
                'password' => 'min:4|confirmed'
            ];
            break;
    }

    return Validator::make($input, $rules);
}

Then in the controller:

    $input      = Input::all();
    $validation = $user->validate($input, 'update', $user->id);

    if ($validation->fails())
    {
        // Do stuff
    }
    else
    {
        // Validation passes
        // Do other stuff
    }

As others mentioned, the 3rd parameter of the unique rule specifies an id to ignore. You can add other cases, such as 'login' to reuse the validation function.

Alternatively, Jeffrey Way at Tuts Premium has a great series of lessons in "What's New In Laravel 4" which includes a couple of other approaches to handling validation using services and listeners.

like image 39
Orphaned Record Avatar answered Oct 30 '22 03:10

Orphaned Record


See the documentation on http://four.laravel.com/docs/validation#rule-unique

You can exclude the users own id

protected $rules= [
    'name'    => 'required',
    'email'    => [
        'required',
        'unique:user,email,THE_USERS_USER_ID',
        'email'
    ]
];
like image 1
JackPoint Avatar answered Oct 30 '22 05:10

JackPoint


As of 2014-01-14, you can use sometimes attribute, I believe Taylor added them 2 days ago to Laravel 4.1

$v = Validator::make($data, array(
    'email' => 'sometimes|required|email',
));

sometimes only validate input if it exists. this may or may not suit your exact scenario, if you don't have a default value for insert.

http://laravel.com/docs/validation#conditionally-adding-rules

like image 1
bitinn Avatar answered Oct 30 '22 05:10

bitinn