F
Filament3mo ago
frame

Making an empty RichEditor field save as null

Has anyone figured out how to make an empty RichEditor field save as null in the database? To avoid empties saving as
{"type": "doc", "content": [{"type": "paragraph", "content": []}]}
{"type": "doc", "content": [{"type": "paragraph", "content": []}]}
as those render into <p></p> so its a bit weird to check whether the value exists.
2 Replies
frame
frameOP3mo ago
public function saving(Model $model): bool
{
if ($model->text === ['type' => 'doc', 'content' => [['type' => 'paragraph', 'content' => []]]]) {
$model->text = null;
}
return true;
}
public function saving(Model $model): bool
{
if ($model->text === ['type' => 'doc', 'content' => [['type' => 'paragraph', 'content' => []]]]) {
$model->text = null;
}
return true;
}
i guess That failed there seem to be other empty json structures, maybe
public function saving(Model $model): bool
{
if ($model->text !== null && RichContentRenderer::make($model->text)->toText() === '') {
$model->text = null;
}
return true;
}
public function saving(Model $model): bool
{
if ($model->text !== null && RichContentRenderer::make($model->text)->toText() === '') {
$model->text = null;
}
return true;
}
Dennis Koch
Dennis Koch2mo ago
You can use something like this:
new TipTap\Editor()
->setContent($model->text)
->getHtml(); // or ->getText()
new TipTap\Editor()
->setContent($model->text)
->getHtml(); // or ->getText()
to convert it back.

Did you find this page helpful?