具有两种可能类型的验证值 [英] validation value having two possible types

查看:59
本文介绍了具有两种可能类型的验证值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当字符串或数组都有效时,如何验证请求值?

How to validate request value when it should be string or array both valid?

'val' => 'bail|required|string'
'val' => 'bail|required|array'

验证表达式是什么?

推荐答案

我不认为有一种方法可以立即使用验证规则来实现.您需要使用自定义验证规则来实现:

I don't think there is a way to achieve it using the validation rules out of the box. You will need to use Custom Validation Rules to achieve this:

AppServiceProvider boot 方法中,添加

public function boot()
{
    Validator::extend('string_or_array', function ($attribute, $value, $parameters, $validator) {
        return is_string($value) || is_array($value);
    });
}

然后您就可以开始使用它了:

Then you can start using it:

'val' => 'bail|required|string_or_array'

(可选)您可以设置自定义验证错误消息,或使用自定义验证类来实现它.请查看上面的doc链接以获取更多信息.

Optionally you can set the custom validation error messages, or using a custom validation class to achieve it. Check out the doc link above for more information.

这篇关于具有两种可能类型的验证值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆