Form method executing before mount method?

i have this class in an EditFilament component:
21 Replies
ζ͜͡Gurthang
ζ͜͡GurthangOP3mo ago
class EditRecordFile extends EditRecord implements HasForms {

use InteractsWithForms;

public ?array $data;

public $expediente = null;
public $expediente_estado = null;
public $expe_id = null;
public $expe_title = null;
public $expe_type = '';
public $expe_org = '';
public $expe_num = '';
public $expe_year = '';
public $expe_program = '';
public $expe_cia = '';

protected static string $resource = RecordFileResource::class;
protected static ?string $title = 'Editar Expedientes';
protected static string $view = 'record-file-resource.pages.edit-record-files';
public function mount($record): void {
parent::mount($record);
logger('Mount ejecutado');

$this->expe_id = $this->record->id;
$this->expediente = Expediente::with('expedienteEstado')->find($this->expe_id);
if ($this->expediente) {
$this->expe_title = $this->expediente->expe_title;
$this->expe_type = $this->expediente->expe_type;
$this->expe_org = $this->expediente->expe_org;
$this->expe_num = $this->expediente->expe_num;
$this->expe_program = $this->expediente->expe_program;
$this->expe_cia = $this->expediente->expe_cia;
$this->expe_year = $this->expediente->expe_year;

// Inicializa el formulario con los datos cargados
$this->form->fill([
'expe_title' => $this->expe_title,
'expe_type' => $this->expe_type,
'expe_org' => $this->expe_org,
'expe_num' => $this->expe_num,
'expe_year' => $this->expe_year,
'expe_program' => $this->expe_program,
'expe_cia' => $this->expe_cia,
]);
}
else {
dd('Expediente no encontrado');
}
}
class EditRecordFile extends EditRecord implements HasForms {

use InteractsWithForms;

public ?array $data;

public $expediente = null;
public $expediente_estado = null;
public $expe_id = null;
public $expe_title = null;
public $expe_type = '';
public $expe_org = '';
public $expe_num = '';
public $expe_year = '';
public $expe_program = '';
public $expe_cia = '';

protected static string $resource = RecordFileResource::class;
protected static ?string $title = 'Editar Expedientes';
protected static string $view = 'record-file-resource.pages.edit-record-files';
public function mount($record): void {
parent::mount($record);
logger('Mount ejecutado');

$this->expe_id = $this->record->id;
$this->expediente = Expediente::with('expedienteEstado')->find($this->expe_id);
if ($this->expediente) {
$this->expe_title = $this->expediente->expe_title;
$this->expe_type = $this->expediente->expe_type;
$this->expe_org = $this->expediente->expe_org;
$this->expe_num = $this->expediente->expe_num;
$this->expe_program = $this->expediente->expe_program;
$this->expe_cia = $this->expediente->expe_cia;
$this->expe_year = $this->expediente->expe_year;

// Inicializa el formulario con los datos cargados
$this->form->fill([
'expe_title' => $this->expe_title,
'expe_type' => $this->expe_type,
'expe_org' => $this->expe_org,
'expe_num' => $this->expe_num,
'expe_year' => $this->expe_year,
'expe_program' => $this->expe_program,
'expe_cia' => $this->expe_cia,
]);
}
else {
dd('Expediente no encontrado');
}
}
and my from method:
public function form(Form $form): Form{
dump($this->expediente);
logger('Form executed');
return $form
->schema([
Section::make()
->id('inicio')
->schema([
Group::make()
->schema([
TextInput::make('expe_title')
->label('Título del Expediente')->required(),
Select::make('expe_type')
->label('Tipo de Expediente')
->options(GeneralList::where('code', 1)->pluck('s_value', 'value')->toArray())
->searchable()
->required(),
])->columns(2),
])
->footerActions([
Action::make('inicio')
->disabled((bool)$this->expe_id)
->label('Crear Expediente')
->requiresConfirmation()
->action(function(Action $action) use($form){
if($this->expe_id == null){
$this->inicioPost();
}
else{
Notification::make()
->title('Ya se ha creado este expediente!')
->danger()
->color('danger')
->send();
}
}),
])
])
->statePath('data')
->model($this->expediente);
}
public function form(Form $form): Form{
dump($this->expediente);
logger('Form executed');
return $form
->schema([
Section::make()
->id('inicio')
->schema([
Group::make()
->schema([
TextInput::make('expe_title')
->label('Título del Expediente')->required(),
Select::make('expe_type')
->label('Tipo de Expediente')
->options(GeneralList::where('code', 1)->pluck('s_value', 'value')->toArray())
->searchable()
->required(),
])->columns(2),
])
->footerActions([
Action::make('inicio')
->disabled((bool)$this->expe_id)
->label('Crear Expediente')
->requiresConfirmation()
->action(function(Action $action) use($form){
if($this->expe_id == null){
$this->inicioPost();
}
else{
Notification::make()
->title('Ya se ha creado este expediente!')
->danger()
->color('danger')
->send();
}
}),
])
])
->statePath('data')
->model($this->expediente);
}
but the form is never filled i tested with;
TextInput::make('expe_title')->label('Título del Expediente')->default($this->expe_title)->required(),
TextInput::make('expe_title')->label('Título del Expediente')->default($this->expe_title)->required(),
nothing happend any help will be apreciated! 🥲
LeandroFerreira
LeandroFerreira3mo ago
if you are using attributes, you shouldn't use ->statePath('data')
ζ͜͡Gurthang
ζ͜͡GurthangOP3mo ago
i tested too.... nothing... the strange thing is when i see the logger its shows this:
[2024-09-20 19:20:15] local.DEBUG: Form ejecutado
[2024-09-20 19:20:15] local.DEBUG: Mount ejecutado
[2024-09-20 19:20:15] local.DEBUG: Form ejecutado
[2024-09-20 19:20:15] local.DEBUG: Mount ejecutado
like the form methos is executing first than mount
LeandroFerreira
LeandroFerreira3mo ago
default only works in the create page. But I think fill form should work.. are you using the form outside the panel, right?
ζ͜͡Gurthang
ζ͜͡GurthangOP3mo ago
outside the panel?
LeandroFerreira
LeandroFerreira3mo ago
I mean, using only the Form builder, not the Panel builder
ζ͜͡Gurthang
ζ͜͡GurthangOP3mo ago
yeah the form builder
LeandroFerreira
LeandroFerreira3mo ago
if you are using a Resource, why are you doing this in the EditPage public function form(Form $form): Form{.. ? Shoudn't you use the Resource?
ζ͜͡Gurthang
ζ͜͡GurthangOP3mo ago
cause i have a view with tabs and in every tab i have diferent tables and forms
ζ͜͡Gurthang
ζ͜͡GurthangOP3mo ago
as long as i know the EditRecordFile is a livewire component i may be wrong
LeandroFerreira
LeandroFerreira3mo ago
there is no reason to edit the edit page for me.. you could use form tabs and render custom LW components..
ζ͜͡Gurthang
ζ͜͡GurthangOP3mo ago
u mean that i make another livewire component to add the form like i did with the others livewire components? may be u are rigth but when i did the createRecordFile it worked fine.... so i didnt think of it
LeandroFerreira
LeandroFerreira3mo ago
I mean, this in your resource..
public static function form(Form $form): Form
{
return $form
->schema([
Tabs::make('tabs')
->tabs([
Tabs\Tab::make('Tab1')
->schema([
Livewire::make(YourLW1::class),
]),
Tabs\Tab::make('Tab2')
->schema([
Livewire::make(YourLW2::class),
]),
])
...
public static function form(Form $form): Form
{
return $form
->schema([
Tabs::make('tabs')
->tabs([
Tabs\Tab::make('Tab1')
->schema([
Livewire::make(YourLW1::class),
]),
Tabs\Tab::make('Tab2')
->schema([
Livewire::make(YourLW2::class),
]),
])
...
ζ͜͡Gurthang
ζ͜͡GurthangOP3mo ago
nop that is in my EditRecordFile i dont have any form in my RecordFileResource
ζ͜͡Gurthang
ζ͜͡GurthangOP3mo ago
No description
LeandroFerreira
LeandroFerreira3mo ago
probably because you remove it 🤷‍♂️
ζ͜͡Gurthang
ζ͜͡GurthangOP3mo ago
yeah i deleted... cause i need like X forms well a step foward.... i did the livewire components and now i can show the expediente information but....
ζ͜͡Gurthang
ζ͜͡GurthangOP3mo ago
only like this
No description
ζ͜͡Gurthang
ζ͜͡GurthangOP3mo ago
my form method now look like this
public function form(Form $form): Form{
dump($this->expediente);
return $form
->schema([
Section::make()
->id('inicio')
->schema([
Group::make()
->schema([
TextInput::make('expe_title')
->label('Título del Expediente')->required(),
Select::make('expe_type')
->label('Tipo de Expediente')
->options(GeneralList::where('code', 1)->pluck('s_value', 'value')->toArray())
->searchable()
->required(),
])->columns(2),
])
])->model($this->expediente);
}
public function form(Form $form): Form{
dump($this->expediente);
return $form
->schema([
Section::make()
->id('inicio')
->schema([
Group::make()
->schema([
TextInput::make('expe_title')
->label('Título del Expediente')->required(),
Select::make('expe_type')
->label('Tipo de Expediente')
->options(GeneralList::where('code', 1)->pluck('s_value', 'value')->toArray())
->searchable()
->required(),
])->columns(2),
])
])->model($this->expediente);
}
still i cant show the data I SOLVED IT!! here is the answer!
class EditFormInicio extends Component implements HasForms {

use InteractsWithForms;

public Expediente $expediente;
public $expediente_estado = null;
public $expe_id = null;
public $data = [];

public function mount($expe_id): void {
$this->expe_id = $expe_id;
$this->expediente = Expediente::with('expedienteEstado')->find($this->expe_id);
$this->form->fill($this->expediente->toArray());
}
public function form(Form $form): Form{
return $form
->schema([
TextInput::make('titulo')
->label('Título del Expediente')
->default($this->expediente['titulo'])
->required(),
Select::make('tipo_cod')
->label('Tipo de Expediente')
->options(GeneralList::where('code', 1)->pluck('s_value', 'value')->toArray())
->searchable()
->required(),
])
->columns(2)
->statePath('data');
}

public function getFormStatePath(): ?string{
return null;
}

public function render()
{
return view('livewire.edit-form-inicio');
}
}
class EditFormInicio extends Component implements HasForms {

use InteractsWithForms;

public Expediente $expediente;
public $expediente_estado = null;
public $expe_id = null;
public $data = [];

public function mount($expe_id): void {
$this->expe_id = $expe_id;
$this->expediente = Expediente::with('expedienteEstado')->find($this->expe_id);
$this->form->fill($this->expediente->toArray());
}
public function form(Form $form): Form{
return $form
->schema([
TextInput::make('titulo')
->label('Título del Expediente')
->default($this->expediente['titulo'])
->required(),
Select::make('tipo_cod')
->label('Tipo de Expediente')
->options(GeneralList::where('code', 1)->pluck('s_value', 'value')->toArray())
->searchable()
->required(),
])
->columns(2)
->statePath('data');
}

public function getFormStatePath(): ?string{
return null;
}

public function render()
{
return view('livewire.edit-form-inicio');
}
}
in a Livewire Component
Want results from more Discord servers?
Add your server