PHP code example of camya / filament-import-inline
1. Go to this page and download the library: Download camya/filament-import-inline library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
camya / filament-import-inline example snippets
use Camya\Filament\Forms\Components\ImportInlineInput;
use Camya\Laravel\Importer\Facades\Import;
class PostResource extends Resource
{
public static function form(Form $form): Form
{
return $form->schema([
ImportInlineInput::make('Import')
->afterStateUpdated(
function ($state, Closure $set, ImportInlineInput $component): void {
$validator = $component->validator();
// Try to import JSON from given state
try {
$importedData = Import::jsonString($state);
} catch (\Exception $e) {
$validator->setValidationError($e->getMessage());
}
// Validate imported data.
$validatedData = $validator->validate(
data: $importedData,
rules: [
'title' => [
'ml('Example JSON: {"title":"Lorem","slug":"ipsum","content":"Hello"}')
->dataHelperLink('https://www.camya.com/', 'Help')
}
}
// Attempt to import JSON from the specified state.
try {
$importedData = Import::jsonString($state);
} catch (\Exception $e) {
$validator->setValidationError($e->getMessage());
}
// Incorrect input format, not a valid JSON string.
Import::JSON_ERROR_INVALID_INPUT
// Valid JSON, but result is not an array.
// If the input is an integer, it's technically a valid JSON object.
// We throw an exception nevertheless, because importing integers
// is not the use case of the importJSON method.
Import::JSON_ERROR_INVALID_RESULT
// Attempt to import CSV from the specified state.
try {
$importedData = Import::csvString($state);
} catch (\Exception $e) {
$validator->setValidationError($e->getMessage());
}
// Empty input data
Import::CSV_ERROR_INVALID_INPUT
// Value count does not matche the value count set in $valuePerRow.
Import::CSV_ERROR_INVALID_CVS_PER_ROW_COUNT
// Incorrect input format, not a valid JSON string.
Import::JSON_ERROR_INVALID_INPUT
// Valid JSON, but result is not an array.
// If the input is an integer, it's technically a valid JSON object.
// We throw an exception nevertheless, because importing integers
// is not the use case of the importJSON method.
Import::JSON_ERROR_INVALID_RESULT
// Empty input data
Import::CSV_ERROR_INVALID_INPUT
// Value count does not matche the value count set in $valuePerRow.
Import::CSV_ERROR_INVALID_CVS_PER_ROW_COUNT
ImportInlineInput::make('Import')
// Import / Validate / Set your data here.
->afterStateUpdated(
function ($state, Closure $set, ImportInlineInput $component): void {
// ...
}
)
// Sets the label above the form element.
->label('Inport Data')
// Hide the label above the form element.
->disableLabel()
// Help text for the detail panel.
->dataHelperHtml('Help <strong>text</strong>')
// Link URL and Title for the detail panel.
->dataHelperLink('https://www.camya.com/', 'Specs')
// Form field placeholder text for the "detail input" field.
->dataPlaceholder('Insert data here.')
// Form field placeholder text for the "paste input" field.
->placeholder('Paste or insert valid data.')
// Deactive listening for "on paste" on "paste input" field.
->insertOnPaste(false)
// Default count of rows of the "paste input" field.
->dataInputRows(2)