简体   繁体   English

Laravel电子邮件验证

[英]Laravel E-mail Validation

How can I validate email address and make them unique? 如何验证电子邮件地址并使其独一无二? my problem is when i save and did not input email it will still stored here is my email 我的问题是,当我保存并且没有输入电子邮件时,它仍然存储在这里是我的电子邮件

my Validation Request 我的验证请求

 $this->validate($request, [ 
        'email_ad' => 'required|email|unique:clients'
    ]);

You need to include the column in the clients table like this: 您需要在clients表中包含列,如下所示:

$this->validate($request, [ 
    'email_ad' => 'required|email|unique:clients,email'
]);

This tells the validation which column to check against 这告诉验证要检查的列

Hope this helps 希望这可以帮助

This help you to validate all the field include email also with customize message 这有助于您验证所有字段包括电子邮件以及自定义消息

 $messages = [
                    'same' => 'The passwords did not match'//customized message
                ];   
         $input = $request->validate([
                        'name' => 'required', //name validation
                        'email' => 'required|email|unique:users,email', //email validation
                        'phone' => 'required|unique:users|min:10|max:12|numeric', //Phone validation
                        'password' => 'required', //password validation
                        'c_password' => 'required|same:password', //confirm Password validation
                        'gender' => 'required', //field validation
                        'referral_code' => 'nullable',
                    ], $messages);

Hope You can understand! 希望你能理解!

Sometimes, you may wish to ignore a given ID during the unique check. 有时,您可能希望在唯一检查期间忽略给定的ID For example, consider an update profile screen that includes the user's name, e-mail address , and location. 例如,考虑update profile屏幕,其中包括用户的名称, e-mail address和位置。 Of course, you will want to verify that the e-mail address is unique . 当然,您需要验证电子邮件地址是否unique However, if the user only changes the name field and not the e-mail field, you do not want a validation error to be thrown because the user is already the owner of the e-mail address. 但是,如果用户仅更改名称字段而不更改电子邮件字段,则不希望抛出validation error ,因为用户已经是电子邮件地址的所有者。

Try to use this approach 尝试使用这种方法

use Illuminate\Validation\Rule;

$this->validate($request, [
    'email_ad' => [
        'required',
        Rule::unique('clients')->ignore($client->id),
    ],
]);

嗨试试这对我有用

 'email' => ['required', 'email', 'max:255', Rule::unique('users')],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM