如何在Laravel验证中验证多个电子邮件? [英] How to validate multiple email in laravel validation?
问题描述
我已经做了所有事情来验证laravel中的变量,但是对于电子邮件,我遇到了一个简单的问题。
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,
'email' => 'required|email'
我知道这仅用于一个电子邮件地址,但是对于,
I got to know this is for only one email address but for like,
email=abc@xyz.com,xyz@abc.com, 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.
谢谢
推荐答案
编写自定义验证程序,它将接收数组并手动验证数组中的每个电子邮件。在Laravel 5 Request中,您可以执行以下操作
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
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'
];
}
这篇关于如何在Laravel验证中验证多个电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!