ModelResource

Form

Basics

In CrudResource (ModelResource) on the formPage, FormBuilder is used, so we recommend that you also check out the FormBuilder section of the documentation.

Validation

Validation is as simple as in FormRequests classes from Laravel.

Just add the rules in the rules() method of the model resource in the usual way.

// torchlight! {"summaryCollapsedIndicator": "namespaces"} // [tl! collapse:3] namespace App\MoonShine\Resources; use MoonShine\Laravel\Resources\ModelResource; class PostResource extends ModelResource { // ... protected function rules(mixed $item): array { return [ 'title' => ['required', 'string', 'min:5'] ]; } }
 namespaces
namespace App\MoonShine\Resources;
 
use MoonShine\Laravel\Resources\ModelResource;
 
class PostResource extends ModelResource
{
// ...
 
protected function rules(mixed $item): array
{
return [
'title' => ['required', 'string', 'min:5']
];
}
}

validation validation_dark

Messages

Using the validationMessages() method, you can create your own validation error messages.

// torchlight! {"summaryCollapsedIndicator": "namespaces"} // [tl! collapse:3] namespace App\MoonShine\Resources; use MoonShine\Laravel\Resources\ModelResource; class PostResource extends ModelResource { // ... public function validationMessages(): array { return [ 'email.required' => 'Email is required' ]; } }
 namespaces
namespace App\MoonShine\Resources;
 
use MoonShine\Laravel\Resources\ModelResource;
 
class PostResource extends ModelResource
{
// ...
 
public function validationMessages(): array
{
return [
'email.required' => 'Email is required'
];
}
}

Preparing Input Data for Validation

If you need to prepare or clean any data from the request before applying validation rules, you can use the prepareForValidation() method.

// torchlight! {"summaryCollapsedIndicator": "namespaces"} // [tl! collapse:3] namespace App\MoonShine\Resources; use MoonShine\Laravel\Resources\ModelResource; class PostResource extends ModelResource { // ... public function prepareForValidation(): void { request()?->merge([ 'email' => request() ?->string('email') ->lower() ->value() ]); } }
 namespaces
namespace App\MoonShine\Resources;
 
use MoonShine\Laravel\Resources\ModelResource;
 
class PostResource extends ModelResource
{
// ...
 
public function prepareForValidation(): void
{
request()?->merge([
'email' => request()
?->string('email')
->lower()
->value()
]);
}
}

Displaying Errors

By default, validation errors are displayed at the top of the form.

The $errorsAbove property is used to control the display of validation errors at the top of the form.

Relevant only if "Asynchronous Mode" is turned off.

// torchlight! {"summaryCollapsedIndicator": "namespaces"} // [tl! collapse:3] namespace App\MoonShine\Resources; use MoonShine\Laravel\Resources\ModelResource; class PostResource extends ModelResource { protected bool $errorsAbove = true; // ... }
 namespaces
namespace App\MoonShine\Resources;
 
use MoonShine\Laravel\Resources\ModelResource;
 
class PostResource extends ModelResource
{
protected bool $errorsAbove = true;
 
// ...
}

Precognition

If it is necessary to perform precognition validation in advance, you need the precognitive() method.

Details in the Laravel documentation.

// torchlight! {"summaryCollapsedIndicator": "namespaces"} // [tl! collapse:3] namespace App\MoonShine\Resources; use MoonShine\Laravel\Resources\ModelResource; class PostResource extends ModelResource { protected bool $isPrecognitive = true; // ... }
 namespaces
namespace App\MoonShine\Resources;
 
use MoonShine\Laravel\Resources\ModelResource;
 
class PostResource extends ModelResource
{
protected bool $isPrecognitive = true;
 
// ...
}

Buttons

To add buttons, use ActionButton and the formButtons() method in the resource.

More about ActionButton.

// torchlight! {"summaryCollapsedIndicator": "namespaces"} // [tl! collapse:2] use MoonShine\Support\ListOf; use MoonShine\UI\Components\ActionButton; protected function formButtons(): ListOf { return parent::formButtons()->add(ActionButton::make('Link', '/endpoint')); }
 namespaces
use MoonShine\Support\ListOf;
use MoonShine\UI\Components\ActionButton;
 
protected function formButtons(): ListOf
{
return parent::formButtons()->add(ActionButton::make('Link', '/endpoint'));
}

Asynchronous Mode

By default, "Asynchronous Mode" is enabled in ModelResource, but if you need to turn it off, set the $isAsync property to false.

// torchlight! {"summaryCollapsedIndicator": "namespaces"} // [tl! collapse:3] namespace App\MoonShine\Resources; use MoonShine\Laravel\Resources\ModelResource; class PostResource extends ModelResource { protected bool $isAsync = false; // ... }
 namespaces
namespace App\MoonShine\Resources;
 
use MoonShine\Laravel\Resources\ModelResource;
 
class PostResource extends ModelResource
{
protected bool $isAsync = false;
 
// ...
}

Modifiers

Components

You can fully replace or modify the resource's FormBuilder for the edit page. To do this, use the modifyFormComponent() method.

// torchlight! {"summaryCollapsedIndicator": "namespaces"} // [tl! collapse:1] use MoonShine\Contracts\UI\ComponentContract; public function modifyFormComponent( ComponentContract $component ): ComponentContract { return parent::modifyFormComponent($component)->customAttributes([ 'data-my-attr' => 'value' ]); }
 namespaces
use MoonShine\Contracts\UI\ComponentContract;
 
public function modifyFormComponent(
ComponentContract $component
): ComponentContract
{
return parent::modifyFormComponent($component)->customAttributes([
'data-my-attr' => 'value'
]);
}