laravel custom validator email code example

Example 1: email validation in laravel

'email' => 'required|email|unique:users,email',
//@sujay

Example 2: username or email validation in laravel

$input = $request->only('account'); // dont use all(), ever
            
$rules = [
  'account' => 'required'
];

if (filter_var($input['account'], FILTER_VALIDATE_EMAIL)) {

  $rules['account'] .= '|exists:users,email';

} else {

  $rules['account'] .= '|exists:users,username';

}

$messages = [
  'account.required' => 'Enter username or email to login.',                
];

$validator = Validator::make($input, $rules, $messages);

Tags:

Php Example