Plugin development - Filament Json Column

Hi, currently working on a new version for a Filament plugin. I want to stop form submission if the state provided is improper. The plugin is a JSON viewer/editor, so I'm trying to parse the state. Here's the code.
protected function setUp(): void
{
parent::setUp();

$this->beforeStateDehydrated(function(FilamentJsonColumn $component, $state) {
if (is_string($state)) {
$decodedState = json_decode($state, true);

if (json_last_error() !== JSON_ERROR_NONE) {
Notification::make()
->title($component->getErrorMessage() === '' ? 'Fix the invalid JSON values' : $component->getErrorMessage())
->danger()
->send();

throw ValidationException::withMessages([]);
}

$component->state($decodedState);
}
});
}
protected function setUp(): void
{
parent::setUp();

$this->beforeStateDehydrated(function(FilamentJsonColumn $component, $state) {
if (is_string($state)) {
$decodedState = json_decode($state, true);

if (json_last_error() !== JSON_ERROR_NONE) {
Notification::make()
->title($component->getErrorMessage() === '' ? 'Fix the invalid JSON values' : $component->getErrorMessage())
->danger()
->send();

throw ValidationException::withMessages([]);
}

$component->state($decodedState);
}
});
}
I'm doing the validation before dehydrating the state, but it feels like a dirty hack, and it's causing issues with dynamic classes in Alpine for a reason I can't seem to understand. Is there a better way to do that, by defining validation rules by default from inside the plugin? EDIT: Managed to solve the Alpine class binding issue, but would still appreciate if there's another supported way to do validation from inside the component.
2 Replies
toeknee
toeknee4w ago
Why not just use a native Laravel Validation Rule?
valentin_morice
valentin_moriceOP4w ago
You're right, calling the rule method in the setup works. Thanks!
protected function setUp(): void
{
parent::setUp();

$this->rule('json');
}
protected function setUp(): void
{
parent::setUp();

$this->rule('json');
}

Did you find this page helpful?