# Lifecycle hooks

Apart from Livewire [defaults](https://laravel-livewire.com/docs/2.x/lifecycle-hooks), TALL-forms offers these additional hooks.

<table data-header-hidden><thead><tr><th width="227.21389742444512">Hooks</th><th>Description</th></tr></thead><tbody><tr><td>Hooks</td><td>Description</td></tr><tr><td><code>updatedFoo($value, $fieldIndexKey)</code></td><td><p><strong>after</strong> field <strong>update</strong>, </p><p><strong>before</strong> real-time <strong>validation</strong></p></td></tr><tr><td><code>updatedFooValidate()</code></td><td><p><strong>after</strong> field <strong>update</strong>, </p><p><strong>after</strong> <code>updatedFoo()</code>, </p><p><strong>replaces</strong> real-time <strong>validation</strong></p></td></tr><tr><td><code>saveFoo($validated_value)</code></td><td><p><strong>After</strong> form submit,</p><p><strong>after</strong> validation<br>To <strong>manually save</strong> form data<br>See <a href="/pages/rNs5bgbJUwoW3flhvo0y">Manually saving data</a></p></td></tr><tr><td><code>onCreateModel($validated_data)</code></td><td><p>On form submit, </p><p>if <code>$model</code> <strong>does not exist.</strong></p></td></tr><tr><td><code>onUpdateModel($validated_data)</code></td><td><p>On form submit, </p><p>if <code>$model</code> <strong>does exist</strong>.</p></td></tr><tr><td><code>onDeleteModel()</code></td><td><p>When the <strong>delete</strong> <strong>button</strong> is clicked. </p><p>There is a <code>$this->defaultDelete()</code> method you can call after performing your access control.</p></td></tr><tr><td><code>saveAndStayResponse()</code></td><td>Save/Submit button</td></tr><tr><td><code>saveAndGoBackResponse()</code></td><td>Save and go back button</td></tr></tbody></table>

### updatedFoo

**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`. 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

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

### saveFoo($validated\_value)

See the [Manually saving](/tall-forms/concept/manually-saving-data.md) data page

### 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

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

### onUpdateModel vs onCreateModel

* When a form is submitted, the TallForm traits calls **one** of these methods based on\
  &#x20;`$model->exists`
* `onCreateModel()` is mandatory, in a form where the model doesn't exist when the form mounts vs `onUpdateModel()`, which is **optional**.

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

### 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

```php
protected function onDeleteModel()
{
    $this->defaultDelete();
}

```

The bundled defaultDelete() method

```php
protected function defaultDelete()
{
    $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));
}
```

### saveAndStayResponse

This method defines the response after successful submission via the `Save/Submit` button. By default it stays on the same page and notifies a message or errors.

Example on how to override it.

```php
protected function saveAndStayResponse()
{
    $this->notify('success');
    return redirect()->route('users.edit', $this->model->id);
}
```

### saveAndGoBackResponse

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:

```php
protected function saveAndGoBackResponse()
{
    return redirect()->route('users.index');
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tina-hammar.gitbook.io/tall-forms/form/lifecycle-hooks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
