我想驗證我的輸入是 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 關注
- 186 瀏覽
添加回答
舉報
0/150
提交
取消