# [Usage](../../../usage.md) > [Components and standard attributes](../basics.md) > action

---
`action` | `->setAction()`

Set a backend action Closure to be executed by a `Button` when click or a `Form` when sent (after validation check). Will overwrite the `URL` if set, or the `onClick` if set.

`action` can only accept a `Closure`, if a simple button:

```php
Button::make()
    ->setAction(function () {
        // Do something here
    })
```

action for `Table` buttons receive the same arguments as the standard table buttons:

```php
Table::make()
    ->setButtons([
        Button::make()
           ->setAction(function ($record, $query, $originalQuery) {
                // Do something here
            }) 
    ])
```

if applied to a `Form`, it receives as arguments the form data:

```php
Form::make()
    ->setAction(function ($data) {
        // Manipulate form $data here
    })
```

### Return types

- Redirects
  ```php
  return redirect();
  ```

- Success and error messages (only in JSON):
  ```php
  return response()->json(['success' => 'success message']);`
  ```

- Files and downloads
  ```php
  return response()->download(
                  $media->getPath(),
                  $media->getCustomProperty('original_name', $media->file_name)
              );
  ```

- HTML to be printed
  ```php
   return response()->json([
      'type' => 'print',
      'html' => view('some-view')->render(),
   ]);
  ```

- Open a modal
  ```php
   return \KFramework\Components\Visual\Modal::make();
  ```
  This will return a Modal that opens automatically at the end of the action.  
  When a modal is returned, a session message can be returned before opening the modal, adding the `ActionEndMessage`:
  ```php
   \KFramework\Components\Visual\Button::make('Do action')
        ->setActionEndMessage('Action done!')
        ->setAction(function () {
            
            // some calculation here
  
            return \KFramework\Components\Visual\Modal::make();
        })
  ```
  `ActionEndMessage` supports:
  - `string`: if string, for example "Action done!" it will fire as success message.
  - `json`: standard messages like `['success' => 'Success message']` or `['error' => 'Error message', 'errorlog' => 'Log of the message']` (errorlog is optional).

  
- Other JSON text

| Types     | Default |
|-----------|---------|
| `Closure` | `null`  |

Backend Closure executed by `Button` on click or `Form` on submit (after validation). Overrides `url` / `onClick` when set. When omitted (`null`) buttons rely on `url` navigation and forms post to `url`. Receives context args (`$record,$query` in tables; `$data` in forms) and can return redirects, JSON messages, downloads, print HTML, or a `Modal`.
Cached per-component `uuid` for execution.

### Example
Blade:
```php
{!! \KFramework\Components\Visual\Button::make('Run')->setAction(fn() => response()->json(['success' => 'Done'])) !!}
```
Controller:
```php
Button::make('Run')->setAction(fn() => response()->json(['success' => 'Done']));
```

### Interaction / tips
- Wins over `url` (../shared_attributes/url.md); gate first with `confirmationMessage` (../shared_attributes/confirmation_message.md).
- On `Form`, `validate` (../shared_attributes/validate.md) runs before `action`.
- Long tasks belong in `progressBar` (../shared_attributes/progress_bar.md), not a single action.

See also: [url](url.md), [confirmationMessage](confirmation_message.md), [validate](validate.md), [progressBar](progress_bar.md), [basics](../basics.md), [Button](../visuals/button.md).