2FAuth/app/Rules/CaseInsensitiveEmailExists.php

26 lines
664 B
PHP
Raw Normal View History

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