I am using Laravel and the Filament v2 library to create a comment form for users. The comment creation process and saving to the database work well, but after submitting a comment through the form, my form disappears and displays the form with an ID like this:
<wire:id="DNrNhGuRgFdKHDeyOPSZ">
Here is the code to call my form:
<div class="p-0 space-y-6 relative mt-2">
<form>
{{ $this->form}}
<div class="flex items-center space-x-2 rounded-b absolute btn-bar">
<button wire:click="submit" type="button" wire:loading.attr="disabled"
class="btn-reply disabled:bg-slate-300 focus:ring-4 focus:outline-none font-medium rounded text-sm px-5 py-1.5 text-center">
{{ $reply?->id ? 'Gửi' : 'Gửi' }}
</button>
@if($reply?->id)
<button type="button" wire:click="cancel()" wire:loading.attr="disabled" class="btn-cancel text-white bg-slate-700 disabled:bg-slate-300 hover:bg-slate-800 focus:ring-4 focus:outline-none focus:ring-slate-300 font-medium rounded text-sm px-5 py-1.5 text-center dark:bg-slate-600 dark:hover:bg-slate-700 dark:focus:ring-slate-800">
Bỏ qua
</button>
@endif
</div>
</form>
</div>
and here is the code to create the form and its submit functions
namespace AppHttpLivewireDiscussion;
use AppCoreNotificationConstants;
use AppCorePointsConstants;
use AppFormsComponentsMentionsRichEditor;
use AppJobsCalculateUserPointsJob;
use AppJobsDispatchNotificationsJob;
use AppModelsDiscussion;
use AppModelsReply as ReplyModel;
use AppModelsUser;
use FilamentFacadesFilament;
use FilamentFormsComponentsRichEditor;
use FilamentFormsConcernsInteractsWithForms;
use FilamentFormsContractsHasForms;
use IlluminateSupportStr;
use LivewireComponent;
class Reply extends Component implements HasForms
{
use InteractsWithForms;
public Discussion $discussion;
public ReplyModel|null $reply = null;
public function mount(): void
{
$data = [];
if ($this->reply) {
$data['content'] = $this->reply->content;
}
$this->form->fill($data);
}
public function render()
{
return view('livewire.discussion.reply');
}
protected function getFormSchema(): array
{
return [
MentionsRichEditor::make('content')
->label('')
->disableAllToolbarButtons()
->enableToolbarButtons([
'bold',
'strike',
'italic',
'blockquote',
'codeBlock',
'attachFiles',
'orderedList',
'bulletList'
])
->placeholder('Bạn nghĩ gì về chủ đề này?')
->mentionsItems(
User::all()
->map(
fn (User $user) => [
'key' => $user->name,
'link' => route('user.index', [
'user' => $user,
'slug' => Str::slug($user->name)
])
])
->toArray()
)
->required()
];
}
public function submit(): void
{
$data = $this->form->getState();
if ($this->reply) {
$this->reply->content = $data['content'];
$this->reply->save();
$message = 'Reply updated successfully';
} else {
ReplyModel::create([
'user_id' => auth()->user()->id,
'discussion_id' => $this->discussion->id,
'content' => $data['content']
]);
$message = 'Reply added successfully';
dispatch(new DispatchNotificationsJob(auth()->user(), NotificationConstants::POST_IN_DISCUSSION->value, $this->discussion));
}
Filament::notify('success', $message);
$this->emit('replyAdded');
$this->emit('replyUpdated');
if ($this->reply) {
$this->dispatchBrowserEvent('replyUpdated');
} else {
$this->dispatchBrowserEvent('replyAdded');
dispatch(new CalculateUserPointsJob(user: auth()->user(), source: $this->reply, type: PointsConstants::NEW_REPLY->value));
}
if ($this->reply) {
$data['content'] = $this->reply->content;
}
$this->form->fill($data);
}
public function cancel(): void
{
$this->emit('replyUpdated');
}
}
This is the image of my form when the page loads
Does anyone have a solution for this? Please help me.
Thank you so much!
I have searched Filament’s documentation but couldn’t find a solution. I’m sorry, as I’m new to this library. Please help me.