Share form problem.

My Form class
final class ProductForm
{
/**
*
* @return array<int, mixed>
*/
public static function getFields(mixed $productId = null): array
{
return [
Select::make('product_id')
->hidden(function () use ($productId) {
return $productId !== null;
})
->relationship('product', 'name')
->required(),
TextInput::make('price')
->required()
->maxLength(55)
];
}
}
final class ProductForm
{
/**
*
* @return array<int, mixed>
*/
public static function getFields(mixed $productId = null): array
{
return [
Select::make('product_id')
->hidden(function () use ($productId) {
return $productId !== null;
})
->relationship('product', 'name')
->required(),
TextInput::make('price')
->required()
->maxLength(55)
];
}
}
From my VendorDetail page which is infolist
public function infolist(Infolist $infolist): Infolist
{
return $infolist->schema([
Section::make('Products')
->collapsible()
->collapsed()
->headerActions([
Actions\Action::make('create_product')
->icon('heroicon-s-user-plus')
->form(ProductForm::getFields($this->record?->id))
->action(function ($data) {
dd($data);
}),
])
]);
}
public function infolist(Infolist $infolist): Infolist
{
return $infolist->schema([
Section::make('Products')
->collapsible()
->collapsed()
->headerActions([
Actions\Action::make('create_product')
->icon('heroicon-s-user-plus')
->form(ProductForm::getFields($this->record?->id))
->action(function ($data) {
dd($data);
}),
])
]);
}
While try to save form It works when I comment product_id select field but in my case this field is hidden.
No description
5 Replies
toeknee
toeknee2mo ago
I'd condition the rendering of select at all rather than using hidden personally. since the getFields is a method which passes the ID you'll know on calling it if it should be there at all.
Asmit Nepali
Asmit NepaliOP2mo ago
@toeknee Thank you for reply Could please give some reference how to render conditionally.
toeknee
toeknee2mo ago
/**
*
* @return array<int, mixed>
*/
public static function getFields(mixed $productId = null): array
{

$fields = [];
if($productId) {
$fields[] = Select::make('product_id')
->relationship('prodcut', 'name')
->required();
}

$fields[] = TextInput::make('price')
->required()
->maxLength(55);


return $fields;
}
/**
*
* @return array<int, mixed>
*/
public static function getFields(mixed $productId = null): array
{

$fields = [];
if($productId) {
$fields[] = Select::make('product_id')
->relationship('prodcut', 'name')
->required();
}

$fields[] = TextInput::make('price')
->required()
->maxLength(55);


return $fields;
}
Kane G
Kane G2mo ago
You do have a spelling mistake "prodcut" should be "product" not sure if that will help or not.
->relationship('prodcut', 'name')
->relationship('prodcut', 'name')
Asmit Nepali
Asmit NepaliOP2mo ago
@Kane G Thank you for your reply, but still I have an issue. I think the solution provided by @toeknee is works for me Thank you, Please suggest me if there any other effect solution
Want results from more Discord servers?
Add your server