F
Filament15mo ago
Bloom

How to combine Wizard and Builder form component

return $form
->schema([
Wizard::make([
Wizard\Step::make('Select Items')
->schema([
Builder::make('content')
->blocks([
Builder\Block::make('Add items')
->schema([
Forms\Components\Select::make('item_id')
->relationship('item', 'item_name')
->required(),
Forms\Components\TextInput::make('quantity')
->required()
->numeric(),
Forms\Components\DatePicker::make('transfer_date')
->required(),
Forms\Components\TextInput::make('notes')
->required()
->maxLength(255),
])
])

]),
Wizard\Step::make('Select Branch')
->schema([
Forms\Components\Select::make('branch_id')
->relationship('branch', 'branch_name')
->required(),
]),
])->submitAction(new HtmlString(Blade::render(<<<BLADE
<x-filament::button
type="submit"
size="sm"
>
Submit
</x-filament::button>
BLADE)))

]);
}
return $form
->schema([
Wizard::make([
Wizard\Step::make('Select Items')
->schema([
Builder::make('content')
->blocks([
Builder\Block::make('Add items')
->schema([
Forms\Components\Select::make('item_id')
->relationship('item', 'item_name')
->required(),
Forms\Components\TextInput::make('quantity')
->required()
->numeric(),
Forms\Components\DatePicker::make('transfer_date')
->required(),
Forms\Components\TextInput::make('notes')
->required()
->maxLength(255),
])
])

]),
Wizard\Step::make('Select Branch')
->schema([
Forms\Components\Select::make('branch_id')
->relationship('branch', 'branch_name')
->required(),
]),
])->submitAction(new HtmlString(Blade::render(<<<BLADE
<x-filament::button
type="submit"
size="sm"
>
Submit
</x-filament::button>
BLADE)))

]);
}
10 Replies
Vp
Vp15mo ago
?
Bloom
BloomOP15mo ago
i am sorry the code i paste is a bit messy, i am creating application where multiple item could be selected using Builder component and i also want to use Wizard Step so that after selecting mutiple item, in the next step i want to select a Branch(which is store branches) so that all those item could be inserted with the Branch store selected. After selecting items and going to the next step it try to insert only the Branch.
Vp
Vp15mo ago
Ok.. what is the error in your case?
Bloom
BloomOP15mo ago
The items selected using Builder is not sent to the database The logic is to create cart like system Is there a better way to achieve this.
Vp
Vp15mo ago
After clicking submit it cannot save directly to database, you need to define the function and save manually.. check below code which is working
public ?array $content = [];

public $email;

public function form(Form $form): Form
{
return $form
->schema([
Wizard::make([
Wizard\Step::make('Select Items')
->schema([
Builder::make('content')
->blocks([
Builder\Block::make('Add items')
->schema([
TextInput::make('name'),
]),
]),
]),
Wizard\Step::make('Select Branch')
->schema([
TextInput::make('email'),
]),
])->submitAction(new HtmlString(Blade::render(<<<'BLADE'
<x-filament::button
wire:click="submit" // change this
>
Submit
</x-filament::button>
BLADE))),
]);
}

// add this
public function submit(): void
{
dd($this->form->getState());
// create your logic to save to DB
}
public ?array $content = [];

public $email;

public function form(Form $form): Form
{
return $form
->schema([
Wizard::make([
Wizard\Step::make('Select Items')
->schema([
Builder::make('content')
->blocks([
Builder\Block::make('Add items')
->schema([
TextInput::make('name'),
]),
]),
]),
Wizard\Step::make('Select Branch')
->schema([
TextInput::make('email'),
]),
])->submitAction(new HtmlString(Blade::render(<<<'BLADE'
<x-filament::button
wire:click="submit" // change this
>
Submit
</x-filament::button>
BLADE))),
]);
}

// add this
public function submit(): void
{
dd($this->form->getState());
// create your logic to save to DB
}
Vp
Vp15mo ago
Result using above code
No description
Bloom
BloomOP15mo ago
Thank you i will try that StockTransfer::create( $this->form->all() ); Is there another way to store the data
Vp
Vp15mo ago
$this->form->getState() Play along this to get data.. for storing you can do whatever you want, search laravel
Bloom
BloomOP15mo ago
$formData = $this->form->getState(); foreach ($formData as $data) { $item_id = $data['item_id']; $quantity = $data['quantity']; $transfer_date = $data['transfer_date']; $notes = $data['notes']; $branch_id = $data['branch_id']; StockTransfer::create([ 'item_id' => $item_id, 'quantity' => $quantity, 'transfer_date' => $transfer_date, 'notes' => $notes, 'branch_id' => $branch_id, ]); } I tried this but i am getting array key not found
Vp
Vp15mo ago
Try this $this->form->getState()['data']
Want results from more Discord servers?
Add your server