Skip to content

Lifecycle Hooks

TinaH edited this page Jul 3, 2021 · 19 revisions

Apart from Livewire default lifecycle hooks see Livewire docs-> this package has a fix and som extra hooks.

updatedFoo($validated_value) //fix for Livewire array field

Executes BEFORE field validation and AFTER field value is updated

The default Livewire way to manipulate data when a field is updated is to add an updatedFieldName() method to your component. This is not working with the form-component because the field is accessed via the field_data array AND Livewire method naming does not support array fields. To get around it, this package has added a fix.

You do NOT have to prefix with FormData

  • Example method name for an array field form_data.people.name will become `updatedPeopleName($validated_value)
  • Example method name for form_data.some_field_name will become `updatedSomeFieldName($validated_value)

Example from above, the form_data.price field would become

public function updatedPrice($validated_value)
{
    $this->form_data['price'] = round($validated_value, 2);
}

onCreateModel($validated_data)

  • Required method if you are creating a new model instance
  • Executes AFTER field validation, intended to CREATE a model
  • The $validated_data contains all fields except custom fields and relational fields
public function onCreateModel($validated_data)
{
    $this->model = User::create($validated_data);
}

onUpdateModel($validated_data)

  • Optional method, the TallForm trait has a default method
  • Executes AFTER field validation, intended to UPDATE an existing model
  • The $validated_data contains all fields except custom fields and relational fields

This is the default method supplied by the TallForm trait

public function onUpdateModel($validated_data)
{
    $this->model->update($validated_data);
}

onDeleteModel()

  • Optional method, the TallForm trait has a default method
  • Executes when clicking the forms Delete button Set component property $showDelete to true, to show the delete button.
  • The hook is invoked if model exists

This is the default method supplied by the TallForm trait

    public function onDeleteModel()
    {
        $className = is_object($this->model) ? get_class($this->model) : "item";
        $this->model->delete();
        session()->flash('success', "The {$className} was deleted");
        return redirect(urldecode($this->previous));
    }

saveFoo($validated_value) //manually saving data

Executes AFTER field validation and AFTER the model is created or updated, meaning that you have access to $this->model even if it is a create form.

All fields can be accessed after the model is saved.
If you have declared any custom fields or relationships they, will be excluded from the default save method,
so you must save them yourself via this method.

You do NOT have to prefix with FormData

  • Example method name for an array field form_data.people.name will become savePeopleName($validated_value)
  • Example method name for form_data.some_field_name will become saveSomeFieldName($validated_value)

Example from above, the form_data.price field would become

    public function savePrice($validated_value)
    {
        $this->model->price = $validated_value;
        $this->model->save();
    }
Clone this wiki locally