284 lines
6.9 KiB
Markdown
284 lines
6.9 KiB
Markdown
# Documentation
|
|
|
|
This document covers package usage, supported fields, responses, authorization, and development notes for `meghdadfadaee/nova-form-card`.
|
|
|
|
## Installation
|
|
|
|
```bash
|
|
composer require meghdadfadaee/nova-form-card
|
|
```
|
|
|
|
The service provider is auto-discovered and registers:
|
|
|
|
- Nova card assets from `dist/js/card.js` and `dist/css/card.css`
|
|
- Submit route at `/nova-vendor/nova-form-card/submit`
|
|
- Sync route for Nova `dependsOn()`
|
|
|
|
Optional publishes:
|
|
|
|
```bash
|
|
php artisan vendor:publish --tag=nova-form-card-config
|
|
php artisan vendor:publish --tag=nova-form-card-translations
|
|
```
|
|
|
|
Translations include PHP backend messages and Nova frontend JSON messages.
|
|
|
|
## Creating A Card
|
|
|
|
Create a class extending `MeghdadFadaee\NovaFormCard\FormCard`.
|
|
|
|
```php
|
|
<?php
|
|
|
|
namespace App\Nova\Cards;
|
|
|
|
use Laravel\Nova\Fields\Text;
|
|
use Laravel\Nova\Http\Requests\NovaRequest;
|
|
use MeghdadFadaee\NovaFormCard\FormCard;
|
|
use MeghdadFadaee\NovaFormCard\FormFields;
|
|
|
|
class InviteUserCard extends FormCard
|
|
{
|
|
public function title(): string
|
|
{
|
|
return __('Invite User');
|
|
}
|
|
|
|
public function fields(NovaRequest $request): array
|
|
{
|
|
return [
|
|
Text::make(__('Email'))->rules('required', 'email'),
|
|
];
|
|
}
|
|
|
|
public function handle(FormFields $fields, NovaRequest $request): mixed
|
|
{
|
|
// Custom submit logic...
|
|
|
|
return static::message(__('Invitation sent.'));
|
|
}
|
|
}
|
|
```
|
|
|
|
Register the card anywhere Nova accepts cards:
|
|
|
|
```php
|
|
use App\Nova\Cards\InviteUserCard;
|
|
use Laravel\Nova\Http\Requests\NovaRequest;
|
|
|
|
public function cards(NovaRequest $request): array
|
|
{
|
|
return [
|
|
new InviteUserCard(),
|
|
];
|
|
}
|
|
```
|
|
|
|
## Card API
|
|
|
|
Common methods:
|
|
|
|
```php
|
|
public function title(): string;
|
|
public function fields(NovaRequest $request): array;
|
|
public function handle(FormFields $fields, NovaRequest $request): mixed;
|
|
public function authorizedToSubmit(NovaRequest $request): bool;
|
|
public function submitButtonText(): string;
|
|
```
|
|
|
|
Per-instance submit text:
|
|
|
|
```php
|
|
(new InviteUserCard())->withSubmitButtonText(__('Send Invite'));
|
|
```
|
|
|
|
## Responses
|
|
|
|
Return one of the helper responses from `handle()`.
|
|
|
|
```php
|
|
return static::message(__('Saved.'));
|
|
return static::success(__('Saved.'));
|
|
return static::error(__('Could not save.'));
|
|
return static::danger(__('Could not save.'));
|
|
return static::refresh(__('Updated.'));
|
|
return static::visit('/resources/users');
|
|
return static::redirect('https://example.com', remote: true);
|
|
```
|
|
|
|
Other return values:
|
|
|
|
- `string`: success message
|
|
- `false`: generic error response
|
|
- `Responsable` or Symfony `Response`: returned directly
|
|
|
|
## Authorization
|
|
|
|
Use Nova's `canSee()` to control whether the card is visible.
|
|
|
|
```php
|
|
(new InviteUserCard())->canSee(
|
|
fn (NovaRequest $request) => $request->user()->can('inviteUsers')
|
|
);
|
|
```
|
|
|
|
Use `authorizedToSubmit()` to protect the submit endpoint.
|
|
|
|
```php
|
|
public function authorizedToSubmit(NovaRequest $request): bool
|
|
{
|
|
return $request->user()->can('inviteUsers');
|
|
}
|
|
```
|
|
|
|
The package embeds a signed token when the card is serialized and rechecks submit authorization on every request.
|
|
|
|
## Validation
|
|
|
|
Nova field `rules()` and `creationRules()` are applied where possible.
|
|
|
|
Validation failures return HTTP `422`:
|
|
|
|
```json
|
|
{
|
|
"message": "The given data was invalid.",
|
|
"errors": {
|
|
"email": ["The email field must be a valid email address."]
|
|
}
|
|
}
|
|
```
|
|
|
|
Errors are displayed beside matching fields in the card.
|
|
|
|
## Supported Fields
|
|
|
|
Submit-capable fields:
|
|
|
|
`Text`, `Textarea`, `Select`, `Boolean`, `Number`, `Date`, `DateTime`, `File`, `Image`, `Avatar`, `Audio`, `Email`, `URL`, `Password`, `PasswordConfirmation`, `Hidden`, `ID`, `Currency`, `Color`, `Code`, `Country`, `Timezone`, `MultiSelect`, `BooleanGroup`, `KeyValue`, `Slug`, `Token`.
|
|
|
|
Display-only fields:
|
|
|
|
`Badge`, `Heading`, `Line`, `Stack`, `Sparkline`, `Status`, `Gravatar`, `UiAvatar`.
|
|
|
|
Unsupported fields throw `UnsupportedFieldException` during card serialization.
|
|
|
|
## Field Notes
|
|
|
|
- `FormFields` extends Nova's `ActionFields`.
|
|
- `Password` uses Nova's native fill behavior, so non-empty submitted values are hashed before they appear in `FormFields`.
|
|
- `PasswordConfirmation` validates but does not appear in `FormFields`, matching Nova.
|
|
- File-like fields submit as `Illuminate\Http\UploadedFile` instances. Your `handle()` method owns storage and cleanup.
|
|
- Display-only fields render through Nova native detail/index components and are not validated or submitted.
|
|
- `Slug` is supported as a manual field. `Slug::from()` requires Nova resource preview endpoints and is not supported.
|
|
|
|
## dependsOn
|
|
|
|
Nova `dependsOn()` is supported through the package sync endpoint.
|
|
|
|
```php
|
|
use Laravel\Nova\Fields\FormData;
|
|
use Laravel\Nova\Fields\Number;
|
|
use Laravel\Nova\Fields\Select;
|
|
|
|
Select::make(__('Plan'))->options([
|
|
'starter' => __('Starter'),
|
|
'enterprise' => __('Enterprise'),
|
|
]);
|
|
|
|
Number::make(__('Seats'))
|
|
->hide()
|
|
->dependsOn(['plan'], function (Number $field, NovaRequest $request, FormData $data): void {
|
|
$data->get('plan') === 'enterprise'
|
|
? $field->show()->rules('required', 'integer', 'min:50')
|
|
: $field->hide()->rules('nullable', 'integer');
|
|
});
|
|
```
|
|
|
|
## Demo Card
|
|
|
|
The demo card at `examples/NovaFormCardDemo.php` shows:
|
|
|
|
- basic fields
|
|
- structured fields
|
|
- display-only fields
|
|
- uploads
|
|
- `dependsOn()`
|
|
- password confirmation
|
|
- custom submit text
|
|
- submit authorization
|
|
- all response helpers
|
|
|
|
Register it like any Nova card:
|
|
|
|
```php
|
|
use App\Nova\Cards\NovaFormCardDemo;
|
|
|
|
(new NovaFormCardDemo())->width('full');
|
|
```
|
|
|
|
## Limitations
|
|
|
|
Unsupported in this version:
|
|
|
|
- `Trix`
|
|
- `Markdown`
|
|
- `Repeater`
|
|
- Vapor fields
|
|
- relationship fields
|
|
- custom fields
|
|
|
|
Other limits:
|
|
|
|
- The package does not recreate Nova resource create/update pages.
|
|
- Cards should be container-resolvable; per-instance constructor state is not reconstructed on submit.
|
|
- Existing file delete/download actions are not resource-backed inside form cards.
|
|
|
|
Nova does not expose a fully public API for arbitrary field rendering outside resources/actions. Nova-specific behavior is isolated in:
|
|
|
|
- `MeghdadFadaee\NovaFormCard\Support\NovaFieldSerializer`
|
|
- `MeghdadFadaee\NovaFormCard\Support\NovaFieldValidator`
|
|
- `MeghdadFadaee\NovaFormCard\Support\NovaFieldIntrospector`
|
|
|
|
## Translations
|
|
|
|
Package messages are translatable. Publish translations with:
|
|
|
|
```bash
|
|
php artisan vendor:publish --tag=nova-form-card-translations
|
|
```
|
|
|
|
Card titles, field labels, and custom response messages should use your app translations:
|
|
|
|
```php
|
|
Text::make(__('Email'));
|
|
|
|
return static::message(__('Invitation sent.'));
|
|
```
|
|
|
|
## Development
|
|
|
|
Build assets:
|
|
|
|
```bash
|
|
npm install
|
|
npm run build
|
|
```
|
|
|
|
Run tests:
|
|
|
|
```bash
|
|
composer install
|
|
vendor/bin/pest
|
|
```
|
|
|
|
Before release:
|
|
|
|
```bash
|
|
composer validate --strict
|
|
find . -name '*.php' -not -path './vendor/*' -print0 | xargs -0 -n1 php -l
|
|
npm run build
|
|
vendor/bin/pest
|
|
```
|
|
|
|
Nova is distributed from Laravel's private Composer repository, so local and CI environments need valid Nova credentials.
|