Initial Commit
This commit is contained in:
32
app/Rules/UniqueCifExceptZero.php
Normal file
32
app/Rules/UniqueCifExceptZero.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Lpj\Rules;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Modules\Lpj\Models\Debiture;
|
||||
|
||||
class UniqueCifExceptZero implements ValidationRule
|
||||
{
|
||||
protected $id;
|
||||
|
||||
public function __construct($id = null)
|
||||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the validation rule.
|
||||
*/
|
||||
public function validate(string $attribute, mixed $value, Closure $fail)
|
||||
: void {
|
||||
if ($value !== '0000000000' && $value !== null && Debiture::query()->where($attribute, $value)->when(
|
||||
$this->id,
|
||||
function ($query) {
|
||||
$query->where('id', '!=', $this->id);
|
||||
},
|
||||
)->exists()) {
|
||||
$fail('The :attribute field must be unique.');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user