set {id}
in your rules and str_replace
it with the before passing it to the validator
pure php power
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);
}
}
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.
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);
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community