Validation rules
You're browsing the documentation for an old version of MoonShine. Consider upgrading your project to MoonShine 2.x.
-
Sections
# Basics
Validation is as simple as in the FormRequests
classes provided by Laravel.
You can simply add rules to the rules()
method of the resource in the usual way.
namespace MoonShine\Resources; use MoonShine\Models\MoonshineUser; class PostResource extends Resource{ public static string $model = App\Models\Post::class; public static string $title = 'Articles'; //... public function rules($item): array { return [ 'title' => ['required', 'string', 'min:5'] ]; } //...}
# Messages
Using the validationMessages()
method, you can create your own validation error messages
namespace MoonShine\Resources; use MoonShine\Models\MoonshineUser; class PostResource extends Resource{ public static string $model = App\Models\Post::class; //... public function validationMessages(): array { return [ 'email.required' => 'Required email' ]; } //...}
# Preparing Input For Validation
If you need to prepare or sanitize any data from the request before you apply your validation rules,
you may use the prepareForValidation()
method.
namespace MoonShine\Resources; use MoonShine\Models\MoonshineUser; class PostResource extends Resource{ public static string $model = App\Models\Post::class; //... public function prepareForValidation(): void { request()?->merge([ 'email' => request() ?->string('email') ->lower() ->value() ]); } //...}