Skip to content

v0.5.1

Latest
Compare
Choose a tag to compare
@camya camya released this 19 Feb 08:52

What's Changed

  • Laravel 10 compatibility updates.

About

This package for FilamentPHP adds the form component ImportInlineInput, which allows you to import machine-readable string data directly into your Filament form fields and validate its structure.

ImportInlineInput::make('Import')
    ->afterStateUpdated(
        function ($state, Closure $set, ImportInlineInput $component): void {

            $validator = $component->validator();

            try {
                $importedData = Import::jsonString($state);
            } catch (\Exception $e) {
                $validator->setValidationError($e->getMessage());
            }

            $validatedData = $validator->validate(
                data: $importedData,
                rules: [
                    'title' => [
                        'required',
                    ],
                    'slug' => [
                        'required',
                    ],
                    'content' => [
                        'required',
                    ],
                ],
                messages: [
                    'title.required' => 'title is required',
                    'slug.required' => 'slug is required',
                    'content.required' => 'content is required',
                ]
            );

            $set('title', $validatedData['title']);
            $set('slug', $validatedData['slug']);
            $set('content', $validatedData['content']);

            $component->statusMessage('Data imported <strong>successful!</strong>');

        }
    )
    ->dataHelperHtml('Example JSON: {"title":"Lorem","slug":"ipsum","content":"Hello"}')
    ->dataHelperLink('https://www.camya.com/', 'Help')