F
Filament4mo ago
o.m

How to set values on Fields initially?

I have lots of input fields but I do not know how to set the values and display it to the page on load.
4 Replies
o.m
o.mOP4mo ago
I created a $formState and thats just about it.
public $formState = [];


public function mount(Setting $slug): void
{
$sections = [];

foreach (SettingSection::get() as $section) {
$sections[$section->id] = $section->label;
}

$this->group_setting_section = $sections;
$this->group_setting = $slug;

$this->formState = [
'label' => $this->group_setting->label ?? '',
'shortcode' => $this->group_setting->shortcode ?? '',
'description' => $this->group_setting->description ?? '',
'help_text' => $this->group_setting->help_text ?? '',
'section_id' => $this->group_setting->section_id ?? null,
'output' => $this->group_setting->output ?? '',
'default_output' => $this->group_setting->default_output ?? '',
];

}
public $formState = [];


public function mount(Setting $slug): void
{
$sections = [];

foreach (SettingSection::get() as $section) {
$sections[$section->id] = $section->label;
}

$this->group_setting_section = $sections;
$this->group_setting = $slug;

$this->formState = [
'label' => $this->group_setting->label ?? '',
'shortcode' => $this->group_setting->shortcode ?? '',
'description' => $this->group_setting->description ?? '',
'help_text' => $this->group_setting->help_text ?? '',
'section_id' => $this->group_setting->section_id ?? null,
'output' => $this->group_setting->output ?? '',
'default_output' => $this->group_setting->default_output ?? '',
];

}
Added this
$this->form->fill([
'label' => $this->group_setting->label,
'shortcode' => $this->group_setting->shortcode,
'description' => $this->group_setting->description,
'help_text' => $this->group_setting->help_text,
]);
$this->form->fill([
'label' => $this->group_setting->label,
'shortcode' => $this->group_setting->shortcode,
'description' => $this->group_setting->description,
'help_text' => $this->group_setting->help_text,
]);
but still no values on the fields on page load Updated the code still nothing works
o.m
o.mOP4mo ago
No description
awcodes
awcodes4mo ago
Did you apply ->statePath() to the form
o.m
o.mOP4mo ago
Finnaly got it, I was missing that

Did you find this page helpful?