使用Laravel验证来验证UUID [英] Validate UUID with Laravel Validation

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

问题描述

是否存在使用验证规则来验证UUID的内置方法?在"可用的验证规则"文档中,我没有找到任何关于此的内容

Is there a built-in way to validate UUID with a validation rule? I did not found anything about this in the "Available Validation Rules" documentation.

推荐答案

您可以在Laravel中扩展验证器帮助程序以添加自定义验证规则,例如,我已经创建了自己的验证规则以使用regex验证位置,如下所示:

You can extend the validator helper in Laravel to add your custom validation rules, for example I've created my own validation rule to validate location using regex as follow:

Validator::extend('location', function ($attribute, $value, $parameters, $validator) {
    return preg_match('/^-?\d{1,2}\.\d{6,}\s*,\s*-?\d{1,2}\.\d{6,}$/', $value);
});

引用此帖子: PHP preg_match UUID v4

您可以使用UUID正则表达式来创建它,如下所示:

You can the use UUID regex to create it as follows:

Validator::extend('uuid', function ($attribute, $value, $parameters, $validator) {
    return preg_match('/[a-f0-9]{8}\-[a-f0-9]{4}\-4[a-f0-9]{3}\-(8|9|a|b)[a-f0-9]{3‌​}\-[a-f0-9]{12}/', $value);
});

希望这符合您的要求.

这篇关于使用Laravel验证来验证UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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