Skip to content

Commit a87e826

Browse files
committed
refactor: add FluxEditorRule and used it in chat
1 parent 33643ee commit a87e826

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

app/Livewire/ChatPanel.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Models\Enums\ChatMessageType;
66
use App\Models\Legacy\ChatMessage;
7+
use App\Rules\FluxEditorRule;
78
use Illuminate\Support\Collection;
89
use Livewire\Component;
910

@@ -33,9 +34,7 @@ public function render()
3334
public function save()
3435
{
3536

36-
$this->validate(['content' => 'required|min:1']);
37-
38-
$cleanContent = strip_tags((string) $this->content, '<p><br><strong><em><ul><ol><li><a><h1><h2><h3>');
37+
$cleanContent = $this->validate(['content' => ['required', 'min:1', new FluxEditorRule()]])['content'];
3938

4039
ChatMessage::create([
4140
'text' => $cleanContent,

app/Rules/FluxEditorRule.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace App\Rules;
4+
5+
use Closure;
6+
use Illuminate\Contracts\Validation\ValidationRule;
7+
use Illuminate\Translation\PotentiallyTranslatedString;
8+
9+
class FluxEditorRule implements ValidationRule
10+
{
11+
/**
12+
* Run the validation rule.
13+
*
14+
* @param \Closure(string, ?string=): PotentiallyTranslatedString $fail
15+
*/
16+
public function validate(string $attribute, mixed $value, Closure $fail): void
17+
{
18+
$cleanContent = strip_tags((string) $value, '<p><s><br><strong><em><ul><ol><li><a><h1><h2><h3>');
19+
if($cleanContent !== $value) {
20+
$fail(__('errors.flux-editor-malicious-html'));
21+
}
22+
}
23+
}

resources/views/livewire/chat-panel.blade.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,22 @@ class="font-medium text-gray-900">{{ $message->user->name ?? $message->creator_a
4747

4848
<!-- New comment form -->
4949
<div class="mt-6 flex gap-x-3">
50-
5150
<div class="-ml-1 mt-3">
5251
<x-profile-pic/>
5352
</div>
54-
<form action="#" class="relative flex-auto">
53+
<div class="relative flex-auto">
5554
<div class="overflow-hidden">
5655
<flux:editor wire:model="content"/>
57-
</div>
5856

57+
</div>
5958
<div class="py-2 pr-2 pl-3 bottom-0 absolute right-0">
6059
<flux:button wire:click="save" variant="primary" color="indigo" icon="paper-airplane"></flux:button>
6160
</div>
62-
</form>
61+
</div>
6362
</div>
63+
@error('content')
64+
<div class="ml-14 mt-2 text-sm text-red-600">{{ $message }}</div>
65+
@enderror
6466

6567

6668
</div>

0 commit comments

Comments
 (0)