Skip to content

Form Methods

tanthammar edited this page Feb 10, 2021 · 9 revisions

Main Form Component 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

Mandatory component methods

mount_form($model)

see Model Binding docs

fields(),

This method returns an array of Fields to use in the form.

Example:

public function fields()
    {
        return [
            Input::make('Name')->rules('required'),
        ];
    }

onUpdateModel($validated_data) AND/OR onCreateModel($validated_data)

  • 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);

Optional component methods

beforeFormProperties()

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;
    }
}

afterFormProperties()

Executes after form_data is set. Example:

    public function afterFormProperties() {
        $this->form_data['someField'] = //change the value of the populated form data
    }

onDeleteModel()

saveAndStayResponse()

This method defines the response after successful submission via the Save button.

Example:

    public function saveAndStayResponse()
    {
        return redirect()->route('users.edit', $this->model->id);
    }

saveAndGoBackResponse()

  • 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');
}

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(), not needed

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();
}

autoSelectSingleArrayValue(string $arrayName, string $field)

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, perfect for the afterFormProperties() 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);
        }
    }
Clone this wiki locally