F
Filament2y 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
WbzyOP2y 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
Filament2y 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
WbzyOP2y 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
krekas2y ago
wtf? what are you doing? why aren't you just using filament forms? custom page is just a livewire component
Wbzy
WbzyOP2y ago
im building a group chat
No description
krekas
krekas2y ago
so? just use forms
Wbzy
WbzyOP2y ago
ok ill try, thanks for your suggestion !
Patrick Boivin
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
WbzyOP2y ago
when i try to use forms it gives the same error
krekas
krekas2y ago
Don't know how you are using them. Read the docs how to use forms in a livewire component
Wbzy
WbzyOP2y 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
ModestasV2y 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
WbzyOP2y ago
tried it and return the same error
ModestasV
ModestasV2y ago
Okay, do you have anything being done with messages?
Wbzy
WbzyOP2y 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
ModestasV2y 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
ModestasV2y ago
Your flare error tells you about this:
No description
Wbzy
WbzyOP2y ago
omg thanks! yea thats the problem! thank for this broo @.modestasv
ModestasV
ModestasV2y ago
Heh, no worries! Glad it solved your issue

Did you find this page helpful?