Skip to content

Instantly share code, notes, and snippets.

@awcodes
Created September 15, 2023 21:18
Show Gist options
  • Save awcodes/9784de531aea8e3bdba772a55958a999 to your computer and use it in GitHub Desktop.
Save awcodes/9784de531aea8e3bdba772a55958a999 to your computer and use it in GitHub Desktop.
Meta component
<?php
namespace App\Forms\Components;
use Filament\Forms;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\HtmlString;
class Meta
{
public static function make(): Forms\Components\Section
{
return Forms\Components\Section::make('SEO')
->collapsible()
->relationship('seo')
->schema([
Forms\Components\TextInput::make('title')
->label('Title')
->live(debounce: 500)
->helperText(function (?string $state): Htmlable {
$count = strlen($state);
$color = $count > 60 ? 'rgb(var(--warning-500))' : 'inherit';
return new HtmlString('<span style="color: ' . $color . ';">' . $count . ' / 60 characters</span>');
}),
Forms\Components\Textarea::make('description')
->label('Description')
->rows(3)
->live(debounce: 500)
->helperText(function (?string $state): Htmlable {
$count = strlen($state);
$color = $count > 160 ? 'rgb(var(--warning-500))' : 'inherit';
return new HtmlString('<span style="color: ' . $color . ';">' . $count . ' / 160 characters</span>');
}),
Forms\Components\CheckboxList::make('robots')
->options([
'noindex' => 'No Index',
'nofollow' => 'No Follow',
])
->gridDirection('row')
->columns(2)
->afterStateHydrated(function ($component, $state) {
if ($state && ! is_array($state)) {
$state = explode(',', $state);
}
$component->state($state ?? []);
})
->dehydrateStateUsing(function (?array $state): ?string {
return filled($state) ? implode(',', $state) : null;
}),
]);
}
}
@drbyte
Copy link

drbyte commented Oct 8, 2023

Yet another way of firing updated status messages on inputs, but enforcing validation is something I used like this:

            ->rule(
                function () {
                    return static function (string $attribute, $value, Closure $fail) {
                        $count = \str_word_count($value);
                        if ($count < 100) {
                            $fail('The description must contain at least 100 words. You have '.$count.'. Please describe more!');
                        }
                    };
                }
            )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment