Create a Dynamic Modal using PHP and HTMX #1

· darkterminal's blog


Hello Punk! This is the index #1

Create a Dynamic Modal using PHP and HTMX #0

Well, I am still at the point where I left before. Yes! The next part about:

The Head Voice! #

How can I create a modal that display form to create a new supplier in my app and load the content from backend (Yes! PHP) that generate the content, also doing validation and stuff, and removing the modal after the create operation is done!.


After I create a the modal and displaying form with awesome CSS transition (copy & paste) from htmx docs. Then what I need is make that form work with insert action.

The Routing #2 POST #

To make the form work with backend I need to create 1 more route that handle POST request from client to server.

1<?php
2// Filename: /home/darkterminal/workspaces/fck-htmx/routes/web.php
3use Fckin\\core\\Application;
4
5/** @var Application $app  */
6
7$app->router->get('suppliers/create', 'Suppliers@create');
8$app->router->post('suppliers/create', 'Suppliers@create');
9

Myself: Why you don't use match routing method? Like:

1$app-router->match(['GET','POST'], 'suppliers/create', 'Suppliers@create')
2

Aaaah... I won't! If you want, create for yourself! (btw, it's not available in my fck PHP Framework, cz I am too egoist). Don't ask about the my fck PHP Framework documentation, they doesn't exists.

The Controller #2 POST #

Still in the same file, but need lil tweak! ✨

The controller before

 1<?php
 2// Filename: /home/darkterminal/workspaces/fck-htmx/controllers/Suppliers.php
 3
 4namespace App\\controllers;
 5
 6use App\\config\\helpers\\Utils;
 7use App\\models\\Suppliers as ModelsSuppliers;
 8use Fckin\\core\\Controller;
 9use Fckin\\core\\Request;
10use Fckin\\core\\Response;
11
12class Suppliers extends Controller
13{
14    protected $suppliers;
15
16    public function __construct()
17    {
18        $response = new Response();
19        if (!isAuthenticate()) {
20            $response->setStatusCode(401);
21            exit();
22        }
23        $this->suppliers = new ModelsSuppliers();
24    }
25
26public function create(Request $request)
27    {
28        $params = []; // <-- I will do it something here when make POST request
29
30        return Utils::addComponent('suppliers/modals/modal-create', $params);
31    }
32}
33

The controller after

 1<?php
 2// Filename: /home/darkterminal/workspaces/fck-htmx/controllers/Suppliers.php
 3
 4namespace App\\controllers;
 5
 6use App\\config\\helpers\\Utils;
 7use App\\models\\Suppliers as ModelsSuppliers;
 8use Fckin\\core\\Controller;
 9use Fckin\\core\\Request;
10use Fckin\\core\\Response;
11
12class Suppliers extends Controller
13{
14    protected $suppliers;
15
16    public function __construct()
17    {
18        $response = new Response();
19        if (!isAuthenticate()) {
20            $response->setStatusCode(401);
21            exit();
22        }
23        $this->suppliers = new ModelsSuppliers();
24    }
25
26    public function create(Request $request)
27    {
28        $params = [
29            'errors' => []
30        ];
31        if ($request->isPost()) {
32            $formData = $request->getBody();
33            $formData['supplierCoordinate'] = empty($formData['latitude']) || empty($formData['latitude']) ? null : \\implode(',', [$formData['latitude'], $formData['longitude']]);
34            $formData = Utils::remove_keys($formData, ['latitude', 'longitude']);
35            $this->suppliers->loadData($formData);
36            if ($this->suppliers->validate() && $this->suppliers->create($formData)) {
37                \\header('HX-Trigger: closeModal, loadTableSupplier');
38                 exit();
39            } else {
40                $params['errors'] = $this->suppliers->errors;
41            }
42        }
43
44        return Utils::addComponent('suppliers/modals/modal-create', $params);
45    }
46}
47

Emmm.... deez neat! How the App\\models\\Suppliers as ModelsSuppliers look like? Did you mean the Model? Sure whatever you want! But before going further... I need to breakdown deez controller first.

1$this->suppliers = new ModelsSuppliers();
2

Initialized the supplier model that accessible in the entire Class Controller from the __constructor method. Then I modify the $params variable

1$params = [
2    'errors' => []
3];
4

I add the errors key that store the error messages from validation. The validation part I will explain in the next paragraph, so the story will be inline as possible. Trust me!

1if ($request->isPost())
2

Check if the client request to POST method, if yes then collect the payload from that request using

1$formData = $request->getBody();
2

and store into $formData variable. Because I am handsome and stupid I create field for latitude and longitude separately and don't comment about the ternary if else statement! Pleaaaaseee....

1$formData['supplierCoordinate'] = empty($formData['latitude']) || empty($formData['latitude']) ? null : \\implode(',', [$formData['latitude'], $formData['longitude']]);
2

So I can combine the latitude and longitude as a supplierCoordinate value. Then remove the latitude and longitude from payload cz I don't need anymore.

1$formData = Utils::remove_keys($formData, ['latitude', 'longitude']);
2

Also I load that payload into the model

1$this->suppliers->loadData($formData);
2

So the model can read all the payload and validate the payload

1$this->suppliers->validate()
2

In the background I have the rules for the input. They look like this:

 1public function rules(): array
 2{
 3    return [
 4        'supplierName' => [self::RULE_REQUIRED],
 5        'supplierCompanyName' => [self::RULE_REQUIRED],
 6        'supplierAddress' => [self::RULE_REQUIRED],
 7        'supplierPhoneNumber' => [self::RULE_REQUIRED],
 8        'supplierEmail' => [self::RULE_EMAIL],
 9        'supplierCoordinate' => []
10    ];
11}
12

If validation passed! Then create the new supplier from the payload

1$this->suppliers->create($formData)
2

If both of them is passed then send the trigger event to the client from the backend

1\\header('HX-Trigger: closeModal, loadTableSupplier');
2

Wait! Wait!! Wait!!! Please... tell me where what the loadTableSupplier?! Where is the table!

Sorry for that... I will explain. Please be patient...

The HX-Trigger is the way how htmx communicate between server and client. This trigger place into the response headers. then telling the client to react on that event.

and if one of them (the validate and create) method doesn't passed then

1$params['errors'] = $this->suppliers->errors;
2

get the error messages to the response payload.

1return Utils::addComponent('suppliers/modals/modal-create', $params);
2

the Utils::addComponent is a glue and part of hypermedia content that can deliver to the client instead sending fixed-format JSON Data APIs.

Error Validation Message #

Did you remember the Utils::getValidationError method in my form?

1Utils::getValidationError($errors, 'supplierName')
2

In each form field? Yes, that the draw how server and client exchange the dynamic hypermedia content. Oh My Punk!

Whenever the $errors is empty the message isn't appear in that form, but if the $errors variable is not empty then it will display the validation message.

Where Is The Table? #

Hold on... don't look at me like that! this is wasting my time. I know, when you choose this topic, you willing to read this and wasting your time also.

Here is the table, but don't complain about the Tailwind Classes and everything is wide and long to read to the right side 🤣 Oh My Punk! this is funny... sorry, I regret. Cz this table is look identical with

1 Endpoint Do 5 Things for HTMX DataTable

  1<?php
  2
  3use App\\config\\helpers\\Icons;
  4use App\\config\\helpers\\Utils;
  5
  6$queries = [];
  7parse_str($suppliers['queryString'], $queries);
  8
  9$currentPage = array_replace_recursive($queries, ['page' => 1, 'limit' => $suppliers['totalRows']]);
 10$prevPage = array_replace_recursive($queries, ['page' => $suppliers['currentPage'] - 1, 'search' => '']);
 11$nextPage = array_replace_recursive($queries, ['page' => $suppliers['currentPage'] + 1, 'search' => '']);
 12?>
 13<div class="overflow-x-auto" id="suppliers-table" hx-trigger="loadTableSupplier from:body" hx-get="<?= base_url('suppliers?' . http_build_query(array_merge($currentPage, ['column' => 'suppliers.updatedAt', 'limit' => 10, 'search' => '', 'order' => 'desc']))) ?>" hx-target="this" hx-swap="outerHTML">
 14    <div class="flex absolute justify-center items-center -mt-2 -ml-2 w-full h-full rounded-lg bg-zinc-400 bg-opacity-35 -z-10 htmx-indicator" id="table-indicator"><?= Icons::use('HxIndicator', 'w-24 h-24') ?></div>
 15    <div class="flex flex-row justify-between mb-3">
 16        <h2 class="card-title">Suppliers</h2>
 17        <div class="flex gap-3">
 18            <input type="search" name="search" placeholder="Search here..." id="search" value="<?= $suppliers['search'] ?? '' ?>" class="w-80 input input-sm input-bordered focus:outline-none" hx-get="<?= base_url('suppliers?' . http_build_query(array_merge($currentPage, ['column' => 'suppliers.supplierId']))) ?>" hx-trigger="input changed delay:500ms, search" hx-swap="outerHTML" hx-target="#suppliers-table" hx-indicator="#table-indicator" autocomplete="off" />
 19            <button class="btn btn-sm btn-neutral" hx-get="<?= base_url('suppliers/create') ?>" hx-target="body" hx-swap="beforeend" id="exclude-indicator"><?= Icons::use('Plus', 'h-4 w-4') ?>Create new</button>
 20        </div>
 21    </div>
 22    <table class="table table-zebra table-sm">
 23        <thead>
 24            <tr>
 25                <th <?= Utils::buildHxAttributes('suppliers', $suppliers['queryString'], 'suppliers.supplierId', $suppliers['activeColumn'], 'suppliers-table') ?>>#</th>
 26                <th <?= Utils::buildHxAttributes('suppliers', $suppliers['queryString'], 'suppliers.supplierName', $suppliers['activeColumn'], 'suppliers-table') ?>>Supplier Name</th>
 27                <th <?= Utils::buildHxAttributes('suppliers', $suppliers['queryString'], 'suppliers.supplierCompanyName', $suppliers['activeColumn'], 'suppliers-table') ?>>Company Name</th>
 28                <th <?= Utils::buildHxAttributes('suppliers', $suppliers['queryString'], 'suppliers.supplierPhoneNumber', $suppliers['activeColumn'], 'suppliers-table') ?>>Phone Number</th>
 29                <th <?= Utils::buildHxAttributes('suppliers', $suppliers['queryString'], 'suppliers.supplierAddress', $suppliers['activeColumn'], 'suppliers-table') ?>>Address</th>
 30                <th class="cursor-not-allowed">Status</th>
 31                <th class="cursor-not-allowed">Distance</th>
 32                <th class="cursor-not-allowed">#</th>
 33            </tr>
 34        </thead>
 35        <tbody>
 36            <?php foreach ($suppliers['data'] as $supplier) : ?>
 37                <tr>
 38                    <th>S#<?= $supplier->supplierId ?></th>
 39                    <td><?= $supplier->supplierName ?></td>
 40                    <td><?= $supplier->supplierCompanyName ?></td>
 41                    <td><?= $supplier->supplierPhoneNumber ?></td>
 42                    <td><?= $supplier->supplierAddress ?></td>
 43                    <td><?= $supplier->isActive ? 'Active' : 'Inactive' ?></td>
 44                    <td>
 45                        <?php
 46                        if (!empty($supplier->supplierCoordinate)) {
 47                            $supplierSource = explode(',', $supplier->supplierCoordinate);
 48                            $appSource = explode(',', '-6.444508061297425,111.01966363196293');
 49                            echo round(Utils::haversine(
 50                                [
 51                                    'lat' => $supplierSource[0],
 52                                    'long' => $supplierSource[1],
 53                                ],
 54                                [
 55                                    'lat' => $appSource[0],
 56                                    'long' => $appSource[1],
 57                                ]
 58                            )) . " Km";
 59                        } else {
 60                            echo "Not set";
 61                        }
 62                        ?>
 63                    </td>
 64                    <td>
 65                        <div class="flex gap-2">
 66                            <button class="text-white bg-blue-600 btn btn-xs hover:bg-blue-700 tooltip tooltip-top" data-tip="View Detail" hx-get="<?= base_url('suppliers/detail/' . $supplier->supplierId) ?>" hx-target="body" hx-swap="beforeend" id="exclude-indicator"><?= Icons::use('Eye', 'h-4 w-4') ?></button>
 67                            <button class="text-white bg-green-600 btn btn-xs hover:bg-green-700 tooltip tooltip-top" data-tip="Edit Detail" hx-get="<?= base_url('suppliers/edit/' . $supplier->supplierId) ?>" hx-target="body" hx-swap="beforeend" id="exclude-indicator"><?= Icons::use('Pencil', 'h-4 w-4') ?></button>
 68                            <button class="text-white btn btn-xs <?= $supplier->isActive ? 'bg-gray-600 hover:bg-gray-700' : 'bg-blue-600 hover:bg-blue-700' ?> tooltip tooltip-top" data-tip="<?= $supplier->isActive ? 'Deactived' : 'Activated' ?>" hx-post="<?= base_url($supplier->isActive ? 'suppliers/deactivated/' : 'suppliers/activated/') . $supplier->supplierId . '?' . http_build_query(array_merge($currentPage, ['column' => 'suppliers.supplierId', 'limit' => 10, 'search' => ''])) ?>" hx-target="#suppliers-table" hx-swap="outerHTML" hx-indicator="#table-indicator" hx-confirm="Are you sure?"><?= Icons::use($supplier->isActive ? 'XCircle' : 'CheckCircle', 'h-4 w-4') ?></button>
 69                            <button class="text-white btn btn-xs <?= $supplier->isActive ? 'bg-red-600 hover:bg-red-700' : 'bg-blue-600 hover:bg-blue-700' ?> tooltip tooltip-top" data-tip="Delete" hx-delete="<?= base_url('suppliers/delete/') . $supplier->supplierId . '?' . http_build_query(array_merge($currentPage, ['column' => 'suppliers.supplierId', 'limit' => 10, 'search' => ''])) ?>" hx-target="#suppliers-table" hx-swap="outerHTML" hx-indicator="#table-indicator" hx-confirm="Are you sure want to delete <?= $supplier->supplierName ?>?"><?= Icons::use('Trash', 'h-4 w-4') ?></button>
 70                        </div>
 71                    </td>
 72                </tr>
 73            <?php endforeach; ?>
 74        </tbody>
 75    </table>
 76    <div class="flex flex-row justify-between mt-3">
 77        <p>
 78            Page <span class="font-bold"><?= $suppliers['currentPage'] ?></span> from <span class="font-bold"><?= $suppliers['totalPages'] ?></span> Total <span class="font-bold"><?= $suppliers['totalRows'] ?></span> |
 79            Jump to: <input type="number" name="pageNumber" id="pageNumber" hx-on:change="var url = '<?= base_url('suppliers?' . http_build_query(array_merge($prevPage, ['column' => 'suppliers.supplierId', 'search' => '']))) ?>';
 80                var replacedUrl = url.replace(/page=\\d+/, 'page=' + this.value);
 81                htmx.ajax('GET', replacedUrl, {target: '#suppliers-table', swap: 'outerHTML'})" class="w-12 input input-sm input-bordered" min="1" max="<?= $suppliers['totalPages'] ?>" value="<?= $suppliers['currentPage'] ?>" hx-indicator="#table-indicator" />
 82            Display: <select class="w-48 select select-bordered select-sm" hx-indicator="#table-indicator" hx-on:change="var url = '<?= base_url('suppliers?' . http_build_query(array_merge($prevPage, ['column' => 'suppliers.supplierId']))) ?>'
 83                    var pageNumber = parseInt('<?= $prevPage['page'] ?>') == 0 ? 1 : parseInt('<?= $prevPage['page'] ?>')
 84                    var replacedUrl = url.replace(/limit=\\d+/, 'limit=' + this.value);
 85                    htmx.ajax('GET', replacedUrl.replace(/page=\\d+/, 'page=' + pageNumber), {target: '#suppliers-table', swap: 'outerHTML'})
 86                ">
 87                <option <?= $suppliers['limit'] == 10 ? 'selected' : '' ?> value="10">10 Rows</option>
 88                <option <?= $suppliers['limit'] == 20 ? 'selected' : '' ?> value="20">20 Rows</option>
 89                <option <?= $suppliers['limit'] == 30 ? 'selected' : '' ?> value="30">30 Rows</option>
 90                <option <?= $suppliers['limit'] == 40 ? 'selected' : '' ?> value="40">40 Rows</option>
 91                <option <?= $suppliers['limit'] == 50 ? 'selected' : '' ?> value="50">50 Rows</option>
 92            </select>
 93        </p>
 94        <div class="join">
 95            <button class="join-item btn btn-sm" <?= Utils::hxPagination('suppliers', http_build_query(array_merge($prevPage, ['column' => 'suppliers.supplierId'])), 'suppliers-table') ?> <?= ($suppliers['currentPage'] <= 1) ? 'disabled' : '' ?></button>
 96            <button class="join-item btn btn-sm">Page <?= $suppliers['currentPage'] ?></button>
 97            <button class="join-item btn btn-sm" <?= Utils::hxPagination('suppliers', http_build_query(array_merge($nextPage, ['column' => 'suppliers.supplierId'])), 'suppliers-table') ?> <?= $suppliers['currentPage'] >= $suppliers['totalPages'] ? 'disabled' : '' ?></button>
 98        </div>
 99    </div>
100</div>
101

In that table what I want to highlight is just the first div!

1<div
2	class="overflow-x-auto"
3    id="suppliers-table"
4    hx-trigger="loadTableSupplier from:body"
5    hx-get="<?= base_url('suppliers?' . http_build_query(array_merge($currentPage, ['column' => 'suppliers.updatedAt', 'limit' => 10, 'search' => '', 'order' => 'desc']))) ?>"
6    hx-target="this"
7    hx-swap="outerHTML"
8>
9

🤣 Yes! The hx attributes:

🤯 Boom!!!

The Model #

The model look pretty clean... and I hate it so much cz I forgot how it's work!

Simple things sometime make me blind. But in the chaotic things, I see the pattern. .darkterminal

 1<?php
 2
 3namespace App\\models;
 4
 5use Fckin\\core\\db\\Model;
 6
 7class Suppliers extends Model
 8{
 9    public string $supplierName;
10    public string $supplierCompanyName;
11    public string $supplierAddress;
12    public string $supplierPhoneNumber;
13    public string|null $supplierEmail;
14    public string|null $supplierCoordinate;
15
16    public string $tableName = 'suppliers';
17
18    public function rules(): array
19    {
20        return [
21            'supplierName' => [self::RULE_REQUIRED],
22            'supplierCompanyName' => [self::RULE_REQUIRED],
23            'supplierAddress' => [self::RULE_REQUIRED],
24            'supplierPhoneNumber' => [self::RULE_REQUIRED],
25            'supplierEmail' => [self::RULE_EMAIL],
26            'supplierCoordinate' => []
27        ];
28    }
29
30    public function create(array $data): bool
31    {
32        $created = $this->table($this->tableName)->insert($data);
33        return $created > 0 ? true : false;
34    }
35}
36

You can look at thc-fck core of my PHP Framework about the Fckin\\core\\db\\Model. That's it!

😊 Sorry for wasting your time...