getState without Validation

Hello, is there away to get form->getState() without triggering validation?
22 Replies
LeandroFerreira
$this->form->getRawState()
hdaklue
hdaklueOP2y ago
tried it but returns empty array
LeandroFerreira
Can you share the code please?
hdaklue
hdaklueOP2y 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
what about the php class?
hdaklue
hdaklueOP2y ago
public function submit() { $state = $this->form->getRawState(); dd($state); } while $this->form->getState() works perfectly
hdaklue
hdaklueOP2y ago
yes if something is wrong form->getState() won't work
LeandroFerreira
maybe... did you try $this->data ?
Dennis Koch
Dennis Koch2y ago
What's your form definition?
hdaklue
hdaklueOP2y 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
public function mount(): void
{
$this->form->fill();
}
public function mount(): void
{
$this->form->fill();
}
? wire:submit.prevent="submit" where is the submit method?
hdaklue
hdaklueOP2y ago
still empty
LeandroFerreira
wire:submit.prevent="submit" where is the submit method?
hdaklue
hdaklueOP2y ago
public function submit() { $state = $this->form->getRawState(); dd($state); }
LeandroFerreira
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
hdaklueOP2y ago
you saved the day!!! Thanks!!
LeandroFerreira
I think if you don't have statepath, you should do $this->name, $this->email..
hdaklue
hdaklueOP2y 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
hdaklueOP2y ago
I mean that $this->form->getRawState() will return [] while $this->getState() works fine which might be confusing without digging deeper
LeandroFerreira
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..

Did you find this page helpful?