为什么验证工作错误?

Why validation works wrong?

这是验证规则:

$validator = Validator::make($request->all(), [
            'email' => 'required|array',
            'email.*' => 'required|email'
        ]);

        if ($validator->fails()) {
            throw new ValidationException($validator);
        }

使用 header 发送的数据:application/json, text/plain, */* Content-Type: application/json 我使用客户端发送:

[{"email":"oli35@mail.ru"},{"email":"zakaji-buket@mail.ru"}]

我也试过:

{"emails":["oliss@mail.ru"]}

尝试将其作为数组发送:

[{"email":["oli35@mail.ru","zakaji-buket@mail.ru"]}]

我想你应该试试下面的内容。

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;
    });
}

在你的函数中

$validator = Validator::make($request->all(), [
            'email' => 'required|emails'
        ]);

        if ($validator->fails()) {
            throw new ValidationException($validator);
        }

并像

一样发送数据
{"email":["oli35@mail.ru","zakaji-buket@mail.ru"]}

我没有测试过,但这会对你有所帮助。