如何在 Laravel 验证中验证多封电子邮件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29454798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:17:53  来源:igfitidea点击:

How to validate multiple email in laravel validation?

phpvalidationemaillaravellaravel-validation

提问by Subhod30

I have done all the things for the validation for the variable in laravel but for emails I got one simple problem.

我已经完成了 laravel 中变量验证的所有工作,但是对于电子邮件,我遇到了一个简单的问题。

From doc of Laravel,

来自 Laravel 的文档,

'email' => 'required|email'

I got to know this is for only one email address but for like,

我知道这仅适用于一个电子邮件地址,但对于类似的,

[email protected],[email protected], def@ghi,com

When I send array of the email i still get email is not a valid email. I have done more like,

当我发送电子邮件数组时,我仍然收到电子邮件不是有效电子邮件。我做过更多类似的事情,

'email' => 'required|email|array'

But I still got error. can any body help.

但我仍然有错误。任何身体都可以帮助。

Thanks,

谢谢,

回答by Margus Pala

You need to write custom Validator, which will take the array and validate each ofthe emails in array manually. In Laravel 5 Request you can do something like that

您需要编写自定义验证器,它将获取数组并手动验证数组中的每个电子邮件。在 Laravel 5 Request 你可以做这样的事情

public function __construct() {
    Validator::extend("emails", function($attribute, $value, $parameters) {
        $rules = [
            'email' => 'required|email',
        ];
        foreach ($value as $email) {
            $data = [
                'email' => $email
            ];
            $validator = Validator::make($data, $rules);
            if ($validator->fails()) {
                return false;
            }
        }
        return true;
    });
}

public function rules() {
    return [
        'email' => 'required|emails'
    ];
}

回答by Julian

In 5.6 or above you can define your validator rule as follows:

在 5.6 或更高版本中,您可以按如下方式定义验证器规则:

'email.*' => 'required|email'

'email.*' => 'required|email'

This will expect the emailkey to be an array of valid email addresses.

这将期望email密钥是一组有效的电子邮件地址。

回答by Elby

We can achieve this without custom validation,We can overridden a method prepareForValidation

我们可以在没有自定义验证的情况下实现这一点,我们可以覆盖一个方法 prepareForValidation

protected function prepareForValidation() 
{
   //Here email we are reciving as comma seperated so we make it array
   $this->merge(['email' => explode(',', rtrim($this->email, ','))]);
}

Then above function will call automatically and convert email-ids to array, after that use array validation rule

然后上面的函数会自动调用并将 email-ids 转换为数组,然后使用数组验证规则

public function rules()
 {
     return [
        'email.*' => 'required|email'
     ];
 }

回答by Sunil Sharma

I did it like this. Working good for me. if email or emails ([email protected], [email protected], [email protected]) are coming from a Form like this following custom validator works. This need to be added to - AppServiceProvider.php - file. And new rule is - 'emails'.

我是这样做的。对我来说有效很好。如果电子邮件或电子邮件([email protected][email protected][email protected])来自以下自定义验证器工作的表单。这需要添加到 - AppServiceProvider.php - 文件中。新规则是-“电子邮件”。

    /**
     * emails
     * Note: this validates multiple emails in coma separated string.
     */
    Validator::extend('emails', function ($attribute, $value, $parameters, $validator) {
        $emails = explode(",", $value);
        foreach ($emails as $k => $v) {
            if (isset($v) && $v !== "") {
                $temp_email = trim($v);
                if (!filter_var($temp_email, FILTER_VALIDATE_EMAIL)) {
                    return false;
                }
            }
        }
        return true;
    }, 'Error message - email is not in right format');

And in your controller, it can be used like this:

在您的控制器中,它可以这样使用:

    $this->validate($request, [
        'email_txt_area' => 'emails',
    ]);

回答by Saman

Laravel 5.2 introduced array validation and you can easily validate array of emails :)

Laravel 5.2 引入了数组验证,您可以轻松验证电子邮件数组 :)

All you need is exploding the string to array.

您所需要的只是将字符串分解为数组。

https://laravel.com/docs/5.2/validation#validating-arrays

https://laravel.com/docs/5.2/validation#validating-arrays