protected function mount_form($model): void //see model binding page
protected function formAttr(): array
protected function beforeFormProperties(): void
protected function afterFormProperties(): void
protected function fields(): array
public function resetFormData(): void //reset button
public function render()
protected function autoSelectSingleArrayValue(string $arrayName, string $field): void
// also see Lifecycle Hooks, page
Mandatory methods
mount_form
If the form type requires it, pass a $model instance to the form in mount()
See page.
fields
Where you define the form fields.
protected function fields()
{
return [
Input::make('Name')->rules('required'),
];
}
protected function beforeFormProperties()
{
$condition = true;
if (!$condition) {
session()->flash('negative', 'The condition is required!');
return redirect(route('some_route'));
} else {
$this->model->some_prop = true;
}
}
afterFormProperties
protected function afterFormProperties() {
$this->form_data['someField'] = //change the value of the populated form data
}
resetFormData = Reset button
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();
}
render
This method renders the form component view, you don't need to add it to the component.
If you have to override it, make sure to return $this->formView().
Example:
public function render()
{
// my custom code
return $this->formView();
}
autoSelectSingleArrayValue
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 = Flat array as key => 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, like in the afterFormProperties() method.
Example: If there is only one item in the users array, the user_id field will be auto populated.
public $users;
public function mount()
{
//Pretend this query only returns one model
$this->users = Users::query()->someScope()->select('id', 'name')->get()->pluck('id', 'name')->all();
}
public function afterFormProperties() // the method can only be used after form_data is generated
{
// just pass the attribute name 'users', not $this->users
$this->autoSelectSingleArrayValue('users', 'user_id');
}
protected function fields()
{
return [
Select::make('Author', 'user_id')->options($this->users),
];
}
Form defaults reside in file, override them with the formAttr() method. See or for more examples.
Executes after form_data is set. See another example at the .