I have a OrderResource i want to have different forms for each create and edit order page?

how can i have this functionailty in orderResource i have this function like public static function form(Form $form): Form { return $form->schema((new Pages\CreateOrder())->createForm()); } this is working fine i want to change for edit but it is using this form for both craete and edit pages for edit i want like
public static function form(Form $form): Form { return $form->schema((new Pages\CreateOrder())->createForm()); if(editpage){ return $form->schema((new Pages\EditOrder())->editForm()); } } i want like this
7 Replies
DrByte
DrByte6mo ago
What is different between your desired Create form vs your Edit form? You can use ->visibleOn('create') and hiddenOn('edit') etc to include/exclude things based on the current operation.
Ali Abbas
Ali Abbas6mo ago
@DrByte i have wizards on create form and on edit form i have tabs therefore i want this condition or something like from which i can understand now it is edit module or create ?
DrByte
DrByte6mo ago
You can override the Resource's form() method directly within your CreateOrder and EditOrder classes:
public function form(Form $form): Form
{
return parent::form($form); // TODO: replace this with your desired form for this page
}
public function form(Form $form): Form
{
return parent::form($form); // TODO: replace this with your desired form for this page
}
What I usually do then is convert most of the form's fields to separate static functions that return the field. And then in both of my overridden form() methods I build a schema from an array of function calls to those static methods. That way the fields and their formatting and validation rules etc are kept consistent everywhere I use them. (I sometimes put those static functions in the Resource, and sometimes put them into a Trait ... depends on how much I'm going to use them elsewhere later. Don't overengineer. It doesn't have to be complicated!)
bakriawad
bakriawad6mo ago
you can also check the operation on the form and return the form you want
public function form(Form $form): Form
{
return $form->getOperation() == "create" ? getCreateForm() : getEditForm();
}

private function getCreateForm():Form {
//return a form
}
private function getEditForm():Form {
//return a form
}
public function form(Form $form): Form
{
return $form->getOperation() == "create" ? getCreateForm() : getEditForm();
}

private function getCreateForm():Form {
//return a form
}
private function getEditForm():Form {
//return a form
}
Ali Abbas
Ali Abbas6mo ago
@bakriawad thank you
Tim van Heugten
Tim van Heugten6mo ago
What naming convention do you use for these static functions?
DrByte
DrByte6mo ago
in Resource:
public static function form(Form $form): Form
{
return $form
->schema([
Section::make('Project Details')
->description('Project Info')
->columns(2)
->schema([
static::getCategorySelectField(),
Forms\Components\Select::make('status')
->selectablePlaceholder(false)
->options(SubmissionStatusEnum::class)
->visible(auth()->user()->hasRole('Super-Admin')),
static::getProjectNameField(),
static::getLocationCityStateField(),
Forms\Components\Placeholder::make('formats')->columnSpanFull()
->label('Nomination Format')
->content(new HtmlString(Content::where('view', 'submissions.nomination-formats')->first()->content)),
static::getProjectDescriptionField(),
Forms\Components\Placeholder::make('tips')->columnSpanFull()
->label('Tip – .... blah blah.')
->content(new HtmlString(Content::where('view', 'submissions.tips')->first()->content)),
]),

static::getNominatorSection(),

static::getContractorDetailsSection(),

...
public static function form(Form $form): Form
{
return $form
->schema([
Section::make('Project Details')
->description('Project Info')
->columns(2)
->schema([
static::getCategorySelectField(),
Forms\Components\Select::make('status')
->selectablePlaceholder(false)
->options(SubmissionStatusEnum::class)
->visible(auth()->user()->hasRole('Super-Admin')),
static::getProjectNameField(),
static::getLocationCityStateField(),
Forms\Components\Placeholder::make('formats')->columnSpanFull()
->label('Nomination Format')
->content(new HtmlString(Content::where('view', 'submissions.nomination-formats')->first()->content)),
static::getProjectDescriptionField(),
Forms\Components\Placeholder::make('tips')->columnSpanFull()
->label('Tip – .... blah blah.')
->content(new HtmlString(Content::where('view', 'submissions.tips')->first()->content)),
]),

static::getNominatorSection(),

static::getContractorDetailsSection(),

...
and in the Trait that's imported into various Resources:
public static function getProjectNameField()
{
return TextInput::make('project_name')
->maxLength(150)
->label('Project Name')
->autofocus()
->required()
->debounce(400)
->afterStateUpdated(function (Forms\Contracts\HasForms $livewire, Forms\Components\TextInput $component) {
$livewire->validateOnly($component->getStatePath());
})
->helperText('(blah blah)')
->columnSpanFull();
}
public static function getCategorySelectField()
{
return Select::make('category_id')
->label('Category')
->options(Category::all()->pluck('name', 'id'))
->columns(2)
->helperText('something helpful here')
->required();
}
// etc
public static function getProjectNameField()
{
return TextInput::make('project_name')
->maxLength(150)
->label('Project Name')
->autofocus()
->required()
->debounce(400)
->afterStateUpdated(function (Forms\Contracts\HasForms $livewire, Forms\Components\TextInput $component) {
$livewire->validateOnly($component->getStatePath());
})
->helperText('(blah blah)')
->columnSpanFull();
}
public static function getCategorySelectField()
{
return Select::make('category_id')
->label('Category')
->options(Category::all()->pluck('name', 'id'))
->columns(2)
->helperText('something helpful here')
->required();
}
// etc