F
Filament16mo ago
hdaklue

getState without Validation

Hello, is there away to get form->getState() without triggering validation?
22 Replies
LeandroFerreira
LeandroFerreira16mo ago
$this->form->getRawState()
hdaklue
hdaklueOP16mo ago
tried it but returns empty array
LeandroFerreira
LeandroFerreira16mo ago
Can you share the code please?
hdaklue
hdaklueOP16mo ago
<div> <x-filament::header heading="{{ $title }}" subheading="Plan your campaigns here." /> <form wire:submit.prevent="submit"> {{ $this->form }} <x-filament::button class="mt-4" type="submit"> {{ __('Create EmailCampaign') }} </x-filament::button> </form> </div>
LeandroFerreira
LeandroFerreira16mo ago
what about the php class?
hdaklue
hdaklueOP16mo ago
public function submit() { $state = $this->form->getRawState(); dd($state); } while $this->form->getState() works perfectly
hdaklue
hdaklueOP16mo ago
yes if something is wrong form->getState() won't work
LeandroFerreira
LeandroFerreira16mo ago
maybe... did you try $this->data ?
Dennis Koch
Dennis Koch16mo ago
What's your form definition?
hdaklue
hdaklueOP16mo ago
class Register extends Component implements HasForms { use InteractsWithForms; use PerformsRedirects; public $name = ''; public $email = ''; public $password = ''; public $company = ''; public $terms = false; public function register(): void { $user = (new CreateNewUser())->create($this->form->getState()); Auth()->login($user); $this->redirect($this->redirectTo ?? Filament::getUrl()); } public function render(): View { return view('livewire.auth.register'); } protected function getFormSchema(): array { return [ TextInput::make('name')->autofocus()->required(), TextInput::make('email')->email()->required()->unique('audience', 'email'), TextInput::make('company')->required(), TextInput::make('password')->required()->password(), Checkbox::make('terms')->required( fn () => Jetstream::hasTermsAndPrivacyPolicyFeature() )->hidden( fn () => ! Jetstream::hasTermsAndPrivacyPolicyFeature() )->label( fn () => new HtmlString( 'I agree to the <a class="underline" href="/terms" target="_blank">terms of service</a> and <a href="/privacy" target="_blank">privacy policy</a>.' ) ), ]; } }
LeandroFerreira
LeandroFerreira16mo ago
public function mount(): void
{
$this->form->fill();
}
public function mount(): void
{
$this->form->fill();
}
? wire:submit.prevent="submit" where is the submit method?
hdaklue
hdaklueOP16mo ago
still empty
LeandroFerreira
LeandroFerreira16mo ago
wire:submit.prevent="submit" where is the submit method?
hdaklue
hdaklueOP16mo ago
public function submit() { $state = $this->form->getRawState(); dd($state); }
LeandroFerreira
LeandroFerreira16mo ago
try to remove properties and add this
public ?array $data = [];

protected function getFormStatePath(): string
{
return 'data';
}
public ?array $data = [];

protected function getFormStatePath(): string
{
return 'data';
}
hdaklue
hdaklueOP16mo ago
you saved the day!!! Thanks!!
LeandroFerreira
LeandroFerreira16mo ago
I think if you don't have statepath, you should do $this->name, $this->email..
hdaklue
hdaklueOP16mo ago
for any default values public function mount(): void { $this->lastSaved = now(); // other values $this->form->fill(); } @Leandro Ferreira yes, for short schemas $this->name method gonna work. but for longer schemas it's easier and better to use public ?array $data = []; protected function getFormStatePath(): string { return 'data'; } @Dennis Koch I think documentation should include this case
hdaklue
hdaklueOP16mo ago
I mean that $this->form->getRawState() will return [] while $this->getState() works fine which might be confusing without digging deeper
LeandroFerreira
LeandroFerreira16mo ago
I think the Filament documentation is a good way to understand how Filament works. And if you're up for some more advanced stuff, diving into the source code could be pretty enlightening..
Want results from more Discord servers?
Add your server