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.
Actually, Laravel 5.7 supports UUID validation.
$validation = $this->validate($request, [
'uuid_field' => 'uuid'
]);
Based on documentation.
Laravel 5.6 provides the ramesey/uuid package out of the box now. You can use its "isValid" method now to check for a UUID. I noticed that the regex in the solution above would fail sometimes. I haven't had any issue yet with the package used by Laravel internally.
Validator::extend('uuid', function ($attribute, $value, $parameters, $validator) {
return \Ramsey\Uuid\Uuid::isValid($value);
});
Unrelated to the question but you can now also generate a UUID using the "Str" class from Laravel. It is the reason why ramsey/uuid is now included by default, removing the necessity to include your own regex.
\Illuminate\Support\Str::uuid();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With