Rule trong laravel 10

Rule trong laravel 10

Trong laravel 10 đã có những cải tiến về rule giúp chúng ta code nhanh hơn :

Tạo rule với lệnh : php artisan make:rule PhoneRule

<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Str;

class PhoneRule implements ValidationRule
{
    /**
     * Run the validation rule.
     *
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if(Str::length($value) <10)
        {
            $fail($attribute .'Không được nhỏ hơn 10');
        }
    }
}

Ở bản mới này chúng ta có $fail trả về message khi sai giá trị