F
Filamentβ€’14mo ago
Jr.Pikong

default value in custom page

Hi , how to add default value for textInput in custom page ? I try to use afterStateHydrated still not working.
class AddBudgetPlanner extends Page implements HasForms
{
use InteractsWithForms, InteractsWithActions, HasPageSidebar;

protected static string $resource = BranchResource::class;
protected static string $view = 'filament.resources.branch-resource.pages.create-budget-planner';
public Branch $record;
protected static ?string $model = Branch::class;

public function getBreadcrumb(): ?string
{
return __('Manage Branch Budget Planner');
}

public function form(Form $form): Form
{
return $form->schema([
TextInput::make('code')->afterStateHydrated(fn(TextInput $component) => $component->state('heloss'))
]);
}
class AddBudgetPlanner extends Page implements HasForms
{
use InteractsWithForms, InteractsWithActions, HasPageSidebar;

protected static string $resource = BranchResource::class;
protected static string $view = 'filament.resources.branch-resource.pages.create-budget-planner';
public Branch $record;
protected static ?string $model = Branch::class;

public function getBreadcrumb(): ?string
{
return __('Manage Branch Budget Planner');
}

public function form(Form $form): Form
{
return $form->schema([
TextInput::make('code')->afterStateHydrated(fn(TextInput $component) => $component->state('heloss'))
]);
}
23 Replies
ChesterS
ChesterSβ€’14mo ago
TextInput::make('code')->default("Some value") ?
Jr.Pikong
Jr.PikongOPβ€’14mo ago
@chesters666 this code not wrk in custom page
Quin.
Quin.β€’14mo ago
->placeholder('value') but ->default needs to work to
Jr.Pikong
Jr.PikongOPβ€’14mo ago
->placeholder('value') work , but not ->dafault()
Quin.
Quin.β€’14mo ago
ah alright we learned something new πŸ˜„ probaly because it won't work on the textInput but only on textEntry , so thats why placeholder does work
Jr.Pikong
Jr.PikongOPβ€’14mo ago
yes, but I need work in textInput with default value πŸ˜„
Quin.
Quin.β€’14mo ago
TextInput::make('code' ?? 'Default value') this should work i guess The default is working for me i think there is a problem with your class
Jr.Pikong
Jr.PikongOPβ€’14mo ago
still not work
No description
Jr.Pikong
Jr.PikongOPβ€’14mo ago
No description
Jr.Pikong
Jr.PikongOPβ€’14mo ago
@Shades.
Quin.
Quin.β€’14mo ago
alrighttttt let me look into it Why don't you use from your BranchResource the create?
Jr.Pikong
Jr.PikongOPβ€’14mo ago
BranchResource use in Edit Branch . and Add Budget Planner is custom page .
Jr.Pikong
Jr.PikongOPβ€’14mo ago
GitHub
GitHub - aymanalhattami/filament-page-with-sidebar-project
Contribute to aymanalhattami/filament-page-with-sidebar-project development by creating an account on GitHub.
Jr.Pikong
Jr.PikongOPβ€’14mo ago
this my branchREsource
class BranchResource extends Resource
{
protected static ?string $model = Branch::class;

protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

protected static ?string $navigationGroup = 'Expense';

public static function sidebar(Model $record): FilamentPageSidebar
{
return FilamentPageSidebar::make()
->setTitle($record->name)
->setDescription("Cluster ".$record->cluster->name)
->setNavigationItems([
PageNavigationItem::make('Edit Branch')
->translateLabel()
->url(static::getUrl('edit', ['record' => $record->id]))->icon('heroicon-o-rectangle-stack')
->isActiveWhen(function () {
return request()->routeIs(static::getRouteBaseName() . '.edit');
})
->visible(true)
->group('Branch'),
//
PageNavigationItem::make('Add Budget Planner')
->translateLabel()
->url(static::getUrl('branch.planner', ['record' => $record->id]))->icon('heroicon-o-rectangle-stack')
->isActiveWhen(function () {
return request()->routeIs(static::getRouteBaseName() . '.branch.planner');
})
->visible()
->group('Budget Planner'),

]);
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('cluster_id')
->relationship('cluster', 'name')
->required(),
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\TextInput::make('address')
->required()
->maxLength(255),
]);
}
class BranchResource extends Resource
{
protected static ?string $model = Branch::class;

protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

protected static ?string $navigationGroup = 'Expense';

public static function sidebar(Model $record): FilamentPageSidebar
{
return FilamentPageSidebar::make()
->setTitle($record->name)
->setDescription("Cluster ".$record->cluster->name)
->setNavigationItems([
PageNavigationItem::make('Edit Branch')
->translateLabel()
->url(static::getUrl('edit', ['record' => $record->id]))->icon('heroicon-o-rectangle-stack')
->isActiveWhen(function () {
return request()->routeIs(static::getRouteBaseName() . '.edit');
})
->visible(true)
->group('Branch'),
//
PageNavigationItem::make('Add Budget Planner')
->translateLabel()
->url(static::getUrl('branch.planner', ['record' => $record->id]))->icon('heroicon-o-rectangle-stack')
->isActiveWhen(function () {
return request()->routeIs(static::getRouteBaseName() . '.branch.planner');
})
->visible()
->group('Budget Planner'),

]);
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('cluster_id')
->relationship('cluster', 'name')
->required(),
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\TextInput::make('address')
->required()
->maxLength(255),
]);
}
Quin.
Quin.β€’14mo ago
protected static ?string $navigationGroup = 'Budget planner';
protected static ?string $navigationLabel = 'Add Budget Planner';
protected static ?string $navigationGroup = 'Budget planner';
protected static ?string $navigationLabel = 'Add Budget Planner';
this should work fine for your navigation i will look for you what the problem is
Jr.Pikong
Jr.PikongOPβ€’14mo ago
No description
Quin.
Quin.β€’14mo ago
ahh nevermind xD I don't see why it's not working, does it has the line under TextInput still if you delete the ?? 'default value'
Jr.Pikong
Jr.PikongOPβ€’14mo ago
this is the complete code
<?php


class AddBudgetPlanner extends Page implements HasForms
{
use InteractsWithForms, InteractsWithActions, HasPageSidebar;

protected static string $resource = BranchResource::class;
protected static string $view = 'filament.resources.branch-resource.pages.create-budget-planner';
public Branch $record;
protected static ?string $model = Branch::class;

protected static ?string $navigationGroup = 'Budget planner';
protected static ?string $navigationLabel = 'Add Budget Planner';

public function getBreadcrumb(): ?string
{
return __('Manage Branch Budget Planner');
}

public function form(Form $form): Form
{
return $form->schema([
TextInput::make('code' ?? 'Default value')
]);
}

public function save(): void
{
$this->validate();

DB::transaction(function () {
$oldStatus = $this->record->status;

$this->record->status = $this->status;
$this->record->save();

activity()
->causedBy(auth()->user())
->performedOn($this->record)
->event('manage')
->withProperties([
'status' => [
'old_value' => $oldStatus,
'new_value' => $this->status,
],
])
->log($this->reason);
});
unset($this->status);
unset($this->reason);

Notification::make()
->title('Saved successfully')
->success()
->send();
}
public function saveAction(): Action
{
return Action::make('save')
->action(fn() => $this->save());
}
}
<?php


class AddBudgetPlanner extends Page implements HasForms
{
use InteractsWithForms, InteractsWithActions, HasPageSidebar;

protected static string $resource = BranchResource::class;
protected static string $view = 'filament.resources.branch-resource.pages.create-budget-planner';
public Branch $record;
protected static ?string $model = Branch::class;

protected static ?string $navigationGroup = 'Budget planner';
protected static ?string $navigationLabel = 'Add Budget Planner';

public function getBreadcrumb(): ?string
{
return __('Manage Branch Budget Planner');
}

public function form(Form $form): Form
{
return $form->schema([
TextInput::make('code' ?? 'Default value')
]);
}

public function save(): void
{
$this->validate();

DB::transaction(function () {
$oldStatus = $this->record->status;

$this->record->status = $this->status;
$this->record->save();

activity()
->causedBy(auth()->user())
->performedOn($this->record)
->event('manage')
->withProperties([
'status' => [
'old_value' => $oldStatus,
'new_value' => $this->status,
],
])
->log($this->reason);
});
unset($this->status);
unset($this->reason);

Notification::make()
->title('Saved successfully')
->success()
->send();
}
public function saveAction(): Action
{
return Action::make('save')
->action(fn() => $this->save());
}
}
ChesterS
ChesterSβ€’14mo ago
Can you try this \Filament\Forms\Components\Textarea::make('code')->default("Something") ?
Jr.Pikong
Jr.PikongOPβ€’14mo ago
@ChesterS still not work .
No description
Jr.Pikong
Jr.PikongOPβ€’14mo ago
No description
Jr.Pikong
Jr.PikongOPβ€’14mo ago
@Dan Harrin please can you help me ? or this is a bug ?
Dan Harrin
Dan Harrinβ€’14mo ago
no bug, you just havent read the "Adding a form to a Livewire component" docs. you are missing $this->form->fill() in mount(). also, read the #βœ…β”Šrules, you should not be @ing anyone for help.
Want results from more Discord servers?
Add your server