Validate UUID with Laravel Validation

人盡茶涼 提交于 2020-07-19 03:33:08

问题


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:


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:


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



回答3:


Actually, Laravel 5.7 supports UUID validation.

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

Based on documentation.




回答4:


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!




回答5:


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


来源:https://stackoverflow.com/questions/46682530/validate-uuid-with-laravel-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!