Po laravel 5.5 można tworzyć jesteś właścicielem obiektu niestandardowe reguły poprawności.
Aby utworzyć nową regułę, wystarczy uruchomić komendę rzemieślnik:
php artisan make:rule GreaterThanTen
laravel umieści nową klasę reguły w katalogu
Przykładem niestandardowego reguły walidacji obiekt może wyglądać app/Rules
coś takiego:
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class GreaterThanTen implements Rule
{
// Should return true or false depending on whether the attribute value is valid or not.
public function passes($attribute, $value)
{
return $value > 10;
}
// This method should return the validation error message that should be used when validation fails
public function message()
{
return 'The :attribute must be greater than 10.';
}
}
z reguły niestandardowego zdefiniowane, można używać go w walidacji kontrolera tak:
public function store(Request $request)
{
$request->validate([
'age' => ['required', new GreaterThanTen],
]);
}
Ten sposób jest o wiele lepsza niż stary sposób tworzyć Closures
na AppServiceProvider
klasy
Oto przykład przepisu zwyczaj sprawdzania poprawności do sprawdzania [kompozytowe unikalne kolumny] (http://stackoverflow.com/ pytania/26683762/how-to-add-combined-fields-validator-rule-in-laravel-4/26684043 # 26684043). – Bogdan