Support the ongoing development of Laravel.io →
Database Validation
Last updated 1 year ago.
0

set {id} in your rules and str_replace it with the before passing it to the validator

pure php power

Last updated 1 year ago.
0

no, horrible sollution

Last updated 1 year ago.
0

webdrop said:

no, horrible sollution

care to elaborate?

example: http://laravel.io/bin/dzoD

Last updated 1 year ago.
0

want smth more elegant :)

Last updated 1 year ago.
0

To be honest there is no other way. I don't like to have separate rules for create and update methods, so here's what works for me:

// on the abstract Validator class
protected function forgeUniqueRulesOnUpdate($key)
{
    foreach (static::$rules as $field => $rulesString) {
        static::$rules[$field] = preg_replace('/(unique:([^|,]+))[^|]*/i', '${1},'.$field.','.$key, $rulesString);
    }
}
Last updated 1 year ago.
0

jarektkaczyk

Yeah, share your opinion.
Ended with custom Validator method. Its a shame there is no built in native function for that.
Best way is to do Package to fast include it in new projects via composer but I'm too new for that.

Last updated 1 year ago.
0

Here's how Cartalyst solves validation in their UserRepository class, modifying the rules for different validation scenarios. Note especially the validForUpdate function, which adresses your question I believe:

public function validForCreation(array $data)
{
    $this->rules['password'] = 'required|min:6';
    return $this->validateUser($data);
}

public function validForUpdate($id, array $data)
{
    $model = $this->find($id);
    $this->rules['email'] .= ",{$model->email},email";
    return $this->validateUser($data, $id);
}

public function validForRegistration(array $data)
{
    $this->rules['password'] = 'required|min:6';
    $this->rules['password_confirm'] = 'required|same:password';
    return $this->validateUser($data);
}
Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.