11

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.

1

5 Answers 5

19

Actually, Laravel 5.7 supports UUID validation.

$validation = $this->validate($request, [
    'uuid_field' => 'uuid'
]);

Based on documentation.

2
  • 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 :-( Mar 21, 2019 at 2:07
  • 1
    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 5, 2020 at 9:06
13

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();
1
  • 1
    Put the code Validator::extend() inside of boot() method in AppServiceProvider class.
    – ibnɘꟻ
    Nov 17, 2021 at 3:35
7

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.

2
  • 1
    Noted that there are some invisible unicode chars in the regexp \u200c\u200b
    – Fluff
    Jul 13, 2019 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); Dec 23, 2019 at 13:57
1

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!

0

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']
        ];
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.