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
frameOP2w 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 Koch2w 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?