2021-11-30 17:36:45 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Rules;
|
|
|
|
|
2023-08-01 15:10:58 +02:00
|
|
|
use Closure;
|
2021-11-30 17:36:45 +01:00
|
|
|
use Illuminate\Contracts\Validation\Rule;
|
2023-08-01 15:10:58 +02:00
|
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
2021-11-30 17:36:45 +01:00
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
2023-08-01 15:10:58 +02:00
|
|
|
class CaseInsensitiveEmailExists implements ValidationRule
|
2021-11-30 17:36:45 +01:00
|
|
|
{
|
|
|
|
/**
|
2023-08-01 15:10:58 +02:00
|
|
|
* Run the validation rule.
|
2021-11-30 17:36:45 +01:00
|
|
|
*/
|
2023-08-01 15:10:58 +02:00
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
2021-11-30 17:36:45 +01:00
|
|
|
{
|
|
|
|
$user = DB::table('users')
|
2023-08-01 15:10:58 +02:00
|
|
|
->whereRaw('email = ?' . ('sqlite' === config('database.default') ? ' COLLATE NOCASE' : ''), [strtolower($value)])
|
|
|
|
->first();
|
2021-11-30 17:36:45 +01:00
|
|
|
|
2023-08-01 15:10:58 +02:00
|
|
|
if (! $user) {
|
|
|
|
$fail('validation.custom.email.exists')->translate();
|
|
|
|
}
|
2021-11-30 17:36:45 +01:00
|
|
|
}
|
|
|
|
}
|