rap-2-h
rap-2-h

Reputation: 32038

Validate UUID with Laravel Validation

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.

Upvotes: 12

Views: 28892

Answers (5)

Legionar
Legionar

Reputation: 7607

Actually, Laravel 5.7 supports UUID validation.

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

Based on documentation.

Just a note, it is not working in earlier 5.7 subversions, but for sure working since 5.7.28.

Upvotes: 22

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!

Upvotes: 1

Juha Vehnia
Juha Vehnia

Reputation: 1398

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

Upvotes: 0

Bryse Meijer
Bryse Meijer

Reputation: 327

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();

Upvotes: 13

wshurafa
wshurafa

Reputation: 183

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.

Upvotes: 7

Related Questions