laravel-validator
laravel-validator copied to clipboard
How to Unique Rule To Ignore A Given ID?
hi,I use laravel validator Run into a problem ValidatorInterface::RULE_UPDATE => [ 'nickname' => 'required|unique:table,columnName,id', ] but How to Unique Rule To Ignore table Given id?
same question
Any solution? I have the same question.
try this $this->validator->with($data)->setId($id)->passesOrFail(ValidatorInterface::RULE_UPDATE); In your rules
'username' => 'required|unique:users'
It works. But when I try to add another unique rule, not validate the new. Example:
ValidatorInterface::RULE_UPDATE => [
'cedula' => 'required|unique:trabajadores',
'correo' => 'required|unique:trabajadores',
],
$this->validator->with($data)->setId($id)->passesOrFail(ValidatorInterface::RULE_UPDATE);
This ignores for the cedula field but not for the mail field. Thanks @meihuasannong
It worked this way for me:
protected $rules = [
'name' => 'required|max:255|unique:departments,name,id'
];
public function update(array $data, $id){
$this->validator->with($data)->setId($id)->passesOrFail();
return $this->repository->update($data, $id);
}
Any update on this? Using ->setId($id)
doesn't work as $this->id
is always set to null
for some reason.
setId
doesn't work if your primary key of the table is not id
try this $this->validator->with($data)->setId($id)->passesOrFail(ValidatorInterface::RULE_UPDATE); In your rules
'username' => 'required|unique:users'
3 days trying to make it work and it was simple as that... thanks a lot