F
Filament11mo ago
Wbzy

Livewire Component inside filament Custom page cant use $this->validate()

when i try to use $this->validate it return "array_merge(): Argument #1 must be of type array, null given", when i try to use this livewire component outside custom page it works well. any one know why?
19 Replies
Wbzy
Wbzy11mo ago
My Filament custom page
<x-filament-panels::page>
<div class="grid grid-cols-3 gap-4">
<div class="col-span-3 lg:col-span-1">
{{ $this->discussionInfolist }}
</div>
<livewire:discussion :recordId="$this->creation->id" />
</div>
</x-filament-panels::page>
<x-filament-panels::page>
<div class="grid grid-cols-3 gap-4">
<div class="col-span-3 lg:col-span-1">
{{ $this->discussionInfolist }}
</div>
<livewire:discussion :recordId="$this->creation->id" />
</div>
</x-filament-panels::page>
My Livewire Component
....

#[Rule(['required_without:file'])]
public $message;

#[Rule('nullable')]
public $file;

public function mount(Model $record): void
{
$this->data = $record;
}

public function sendMessage(): void
{
$validated = $this->validate();
...
}
....

#[Rule(['required_without:file'])]
public $message;

#[Rule('nullable')]
public $file;

public function mount(Model $record): void
{
$this->data = $record;
}

public function sendMessage(): void
{
$validated = $this->validate();
...
}
My blade
<form method="POST" wire:submit="sendMessage">
<input wire:model="file" hidden>
<x-filament::input.wrapper>
<x-filament::input
type="text"
wire:model="message"
/>
</x-filament::input.wrapper>
<button type="submit">Submit</button>
</form>
<form method="POST" wire:submit="sendMessage">
<input wire:model="file" hidden>
<x-filament::input.wrapper>
<x-filament::input
type="text"
wire:model="message"
/>
</x-filament::input.wrapper>
<button type="submit">Submit</button>
</form>
it return
array_merge(): Argument #1 must be of type array, null given
array_merge(): Argument #1 must be of type array, null given
Filament
Filament11mo ago
We need more information to help you debug your problem. Please click on the top left 'SHARE' button of the error page you're seeing and share the link with us.
Wbzy
Wbzy11mo ago
Flare
array_merge(): Argument #1 must be of type array, null given - The error occurred at http://kli-new.test/admin/registration/creation-requests/discussion/2
krekas
krekas11mo ago
wtf? what are you doing? why aren't you just using filament forms? custom page is just a livewire component
Wbzy
Wbzy11mo ago
im building a group chat
No description
krekas
krekas11mo ago
so? just use forms
Wbzy
Wbzy11mo ago
ok ill try, thanks for your suggestion !
Patrick Boivin
Patrick Boivin11mo ago
I'm saying this personally, as someone who comes here regularly: I think you are often helpful, but I could do with less bitterness in your approach.
Wbzy
Wbzy11mo ago
when i try to use forms it gives the same error
krekas
krekas11mo ago
Don't know how you are using them. Read the docs how to use forms in a livewire component
Wbzy
Wbzy11mo ago
i literally use it like the docs told
class Discussion extends Component implements HasForms
{
use WithPagination, WithFileUploads, InteractsWithForms;

public ?array $data = [];

public function form(Form $form): Form
{
return $form
->schema([
TextInput::make('message')
->hiddenLabel()
->requiredWithout('file'),
FileUpload::make('file')
->hidden()
->extraAlpineAttributes(['x-ref' => 'file', 'x-on:change' => 'fileMessage = $event.target.files[0];
open = fileMessage ? true : false;']),
])
->statePath('data');
}

public function sendMessage(): void
{
dd($this->form->getState());

}
class Discussion extends Component implements HasForms
{
use WithPagination, WithFileUploads, InteractsWithForms;

public ?array $data = [];

public function form(Form $form): Form
{
return $form
->schema([
TextInput::make('message')
->hiddenLabel()
->requiredWithout('file'),
FileUpload::make('file')
->hidden()
->extraAlpineAttributes(['x-ref' => 'file', 'x-on:change' => 'fileMessage = $event.target.files[0];
open = fileMessage ? true : false;']),
])
->statePath('data');
}

public function sendMessage(): void
{
dd($this->form->getState());

}
in Blade
<form wire:submit="sendMessage">
{{ $this->form }
<button type="submit">Submit</button>
</form>
<form wire:submit="sendMessage">
{{ $this->form }
<button type="submit">Submit</button>
</form>
ModestasV
ModestasV11mo ago
@wbzyfishy Wait, you have an array in your rule!!!! Could you remove the array and keep a string - then check if that's the problem? #[Rule(['required_without:file'])] To: #[Rule('required_without:file')] Or if that doesn't change things - what are you doing with: $messages property or public function messages() Because the flare issue points to those misused
Wbzy
Wbzy11mo ago
tried it and return the same error
ModestasV
ModestasV11mo ago
Okay, do you have anything being done with messages?
Wbzy
Wbzy11mo ago
i dont have public function messages() and my $message property i just use it for saving like this
$data = [
'user_id' => Auth::id(),
'file' => $this?->file,
'creation_request_id' => $this->creationId,
'message' => $this->message,
'created_by' => Auth::id(),
];

Message::create($data);
$data = [
'user_id' => Auth::id(),
'file' => $this?->file,
'creation_request_id' => $this->creationId,
'message' => $this->message,
'created_by' => Auth::id(),
];

Message::create($data);
ModestasV
ModestasV11mo ago
Ahhhhhh So that's your problem! public function messages() is a reserved function you should not use that name. Change this name and everything will work! ps. This function is now being triggered on validation as it attempts to get the message values (array) from this function. So you are always attempting to also create the message in dataabase 😉 Oh sorry, misunderstood - your public $messages is used for that, not the function! It's also reserved keyword
ModestasV
ModestasV11mo ago
Your flare error tells you about this:
No description
Wbzy
Wbzy11mo ago
omg thanks! yea thats the problem! thank for this broo @.modestasv
ModestasV
ModestasV11mo ago
Heh, no worries! Glad it solved your issue
Want results from more Discord servers?
Add your server
More Posts