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.
5 Answers
Actually, Laravel 5.7 supports UUID validation.
$validation = $this->validate($request, [
'uuid_field' => 'uuid'
]);
Based on documentation.

- 7,472
- 2
- 41
- 70
-
Not working for me at least. I think this is bug. For version 5.7 Laravel clearly specifies this should be available but it is not. Maybe this has been fixed in 5.8, can't quite upgrade yet due to package dependencies :-( – Juha Vehnia Mar 21 '19 at 02:07
-
and please note that if you doing something like ```required|uuid|string``` the uuid will not work you should do it as ```required|uuid``` without the string – Fadi Jun 05 '20 at 09:06
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();

- 197
- 2
- 11
-
1Put the code `Validator::extend()` inside of `boot()` method in `AppServiceProvider` class. – ibnɘꟻ Nov 17 '21 at 03:35
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);
});
Referencing this post: PHP preg_match UUID v4
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);
});
Hope that this match your request.

- 183
- 2
- 9
-
1Noted that there are some invisible unicode chars in the regexp `\u200c\u200b` – Fluff Jul 13 '19 at 17:09
-
string without invisible symbols: 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); – Solo.dmitry Dec 23 '19 at 13:57
For the ones that are having problem using the validation uuid method in Laravel 5.7, I fixed by updating Laravel (it was 5.7.6 then after updating 5.7.28) and it worked!
Laravel 5.7 UUID validation is not working for some reason. At least I get
InvalidArgumentException: validation.uuid.
The best way to fix this is to create a rule out of it.
php artisan make:rule UUID
Here is my rule class for UUID validation:
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use Ramsey\Uuid\Uuid as UuidValidator;
class UUID implements Rule
{
/**
* Validate UUID
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return UuidValidator::isValid($value);
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'Supplied :attribute is not valid UUID!';
}
}
Then you can use it manually like this
$validator = Validator::make($data->all(), ['uuid' => new UUID]);
if ($validator->fails()) {
// Do whatever
}
Or use it with http request validation like this
namespace App\Http\Requests;
use App\Rules\UUID;
use Illuminate\Foundation\Http\FormRequest;
class UserRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'uuid' => ['required', new UUID],
'email' => ['required','email']
];
}
}

- 1,255
- 12
- 15