我想验证我的输入是 UUID 或“LAST_QUESTION”。我创建了一个自定义验证规则LastOrUUID,我想在其中使用 UUID 验证规则。我找不到任何方法来做到这一点,也许您知道实现此目的的正确方法是什么?我的上下文自定义规则:class LastOrUUID implements Rule{    /**     * Determine if the validation rule passes.     *     * @param string $attribute     * @param mixed $value     * @return bool     */    public function passes($attribute, $value)    {        if ($value === 'LAST_QUESTION' /* || this is uuid */) {            return true;        }        return false;    }    /**     * Get the validation error message.     *     * @return string     */    public function message()    {        // TODO - add translation        return 'This should either be the last question or have a reference to next question!';    }}
                    
                    
                1 回答
 
                    
                    
                            慕桂英3389331
                            
                                
                            
                        
                        
                                                
                    TA贡献2036条经验 获得超8个赞
如果您只想验证给定值是否是 UUID,您可以使用 Laravel 的本机Str::isUuid方法(在幕后使用 RegEx)、Ramsey 的 UUID 包isValid的方法或普通的 RegEx:
// Laravel native
use Illuminate\Support\Str;
return $value === 'LAST_QUESTION' || Str::isUuid($value);
// Ramsey's package
use Ramsey\Uuid\Uuid;
return $value === 'LAST_QUESTION' || Uuid::isValid($value);
// RegEx
return $value === 'LAST_QUESTION' || 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);
- 1 回答
- 0 关注
- 201 浏览
添加回答
举报
0/150
	提交
		取消
	