PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 24, 2022

[FIXED] Laravel unique email validation is not working on different character case in mongodb

 January 24, 2022     laravel-5, laravel-5.5, php     No comments   

Issue

I have a registration form with unique email validation. When I enter different character case, emails does not apply unique validation.

example@gmail.com, Example@gmail.com, eXample@gmail.com: Laravel unique validation is failed.

example@gmail.com, example@gmail.com, example@gmail.com: Laravel unique validation is success.

Please check my code below and correct me. Email is storing as lowercase in database.

DB - Mongodb,

Framework - Laravel 5.5

jenssegers/laravel-mongodb is using to connect laravel and mongodb

RegisterController.php

protected function validator(array $data)
{
  return Validator::make($data, [
     'firstName' => 'required|string|max:255',
     'lastName' => 'required|string|max:255',
     'email' => 'required|string|email|max:255|unique:user,usrEmail',
     'password' => 'required|string|min:6|confirmed',
  ]);
}

User.php (Model)

public function setusrEmailAttribute($value)
{
  $this->attributes['usrEmail'] = strtolower($value);
}

Solution

With the help of laravel custom validation rule I resolved this issue.

I create a new rule using php artisan command php artisan make:rule Lowercase and defined the rule.

app/Rules/Lowercase.php

public function passes($attribute, $value)
{
 return strtolower($value) === $value;
}

public function message()
{
    return 'The :attribute must be lowercase.';
}

I have attached the rule object with other rules

RegisterController.php

use App\Rules\Lowercase;
protected function validator(array $data)
{
        return Validator::make($data, [
            'firstName' => 'required|string|max:255',
            'lastName' => 'required|string|max:255',
            'email' => [ 'required', 'string', 'email', 'max:255', 'unique:user,usrEmail', new Lowercase ],
            'password' => 'required|string|min:6|confirmed',
            'dataProtection' => 'required',
            'termsService' => 'required',
        ]);
}

While registration users enter email, system will alert the users to use lowercase.

It works for me I am not sure this a good approach or not.



Answered By - Sarath TS
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing