-
-
Notifications
You must be signed in to change notification settings - Fork 86
Form Methods
public function mount_form($model): void //see model binding page
public function beforeFormProperties(): void
public function afterFormProperties(): void
public function fields(): array
public function onUpdateModel($validated_data): void
public function onCreateModel($validated_data): void
public function onDeleteModel(): Application|RedirectResponse|Redirector
public function saveAndStayResponse(): void
public function saveAndGoBackResponse(): Application|RedirectResponse|Redirector
public function resetFormData(): void
public function render(): Application|Factory|View|Mixed
public function autoSelectSingleArrayValue(string $arrayName, string $field): void
see Model Binding docs
This method returns an array of Fields
to use in the form.
Example:
public function fields()
{
return [
Input::make('Name')->rules('required'),
];
}
- When a form is submitted, the TallForm traits calls one of these methods based on
$model->exists
-
onCreateModel()
is mandatory, in a form where the model doesn't exist when the form mounts. - the
onUpdateModel()
is optional. See Lifecycle Hooks.
This is how the form component evaluates which method to call.
filled($this->model) && $this->model->exists ? $this->onUpdateModel($model_fields_data) : $this->onCreateModel($model_fields_data);
Executes before form_data is set. Example:
public function beforeFormProperties()
{
$condition = true;
if (!$condition) {
session()->flash('negative', 'The condition is required!');
return redirect(route('some_route'));
} else {
$this->model->some_prop = true;
}
}
Executes after form_data is set. Example:
public function afterFormProperties() {
$this->form_data['someField'] = //change the value of the populated form data
}
- See
$showDelete
property - See
onDeleteModel()
on the Lifecycle Hooks page - Add your own
onDeleteModel()
method to your component or use the default, provided by this package
This method defines the response after successful submission via the Save
button.
Example:
public function saveAndStayResponse()
{
return redirect()->route('users.edit', $this->model->id);
}
- Also read $showGoBack property
- This method defines the response after successful submission via the
Save & Go Back
button. - By default it returns
redirect()->back()
.
Example on how to override the method:
public function saveAndGoBackResponse()
{
return redirect()->route('users.index');
}
- If you want to override the default reset form method
- You can call this method to reset the form, in any another method
$this->resetFormData();
Default:
public function resetFormData()
{
$this->resetErrorBag();
$this->setFormProperties();
}
This method renders the form component view. If you have to override it, make sure to return $this->formView()
.
OBSERVE: you do not have to override the render method if you're only looking to pass a layout
.
Use the form component $layout
property in the mount()
method instead. Read more on the Wrapper wiki page
public function mount()
{
$this->layout = 'layouts/base';
}
Example:
public function render()
{
// my custom code
return $this->formView();
}
Auto populate a field with an option if there is only one value available. Initially developed for BelongsTo
relationships where a query might return only one model, but can be used for any field as a conditional value.
-
$arrayName
= Flatarray
askey => value
. IMPORTANT: Please note that the method only works with a key/value array. It extracts the value, with php array_values(), and uses it to populate the field. -
$field
= String. The name of the field that should be auto populated. -
IMPORTANT NOTE: This method can only be called after
form_data
is set, perfect for theafterFormProperties()
method.
Example: If there is only one item in the users array, the user_id field will be auto populated.
public $users;
// in the mount() method. Pretend this query only returns one model
$this->users = Users::query()->someScope()->select('id', 'name')->get()->pluck('id', 'name')->all();
// can only be used after form_data is generated
public function afterFormProperties()
{
// just pass the string 'users', not $this->users
$this->autoSelectSingleArrayValue('users', 'user_id');
}
// in the fields() method
Select::make('Author', 'user_id')->options($this->users),
The method declaration
public function autoSelectSingleArrayValue(string $arrayName, string $field)
{
if (count($this->$arrayName) === 1) {
$this->form_data[$field] = array_values($this->$arrayName);
}
}
- Installation
- Requirements
- v5 Upgrade Guide
- v6 Upgrade Guide
- v7 Upgrade Guide
- Support
- Quickstart
- Manual installation
- Optional
- Form component
- Field
- Field types
- Example Form
- Blade Components
- Notifications