Skip to content

Commit

Permalink
feat(tree_types): Validation Tree Types (#150)
Browse files Browse the repository at this point in the history
This pull request includes several changes to improve the admin layout,
tree type model, and tree type views. The most important changes include
moving the JavaScript import in the admin layout, removing the
`photo_id` field from the `TreeType` model, updating the tree type
creation and edit forms, and adding client-side validation for the tree
type form.

Admin layout updates:

* Moved the `app.js` script import to the end of the body in
`AdminLayout.php` to improve page load performance.
[[1]](diffhunk://#diff-e43b091c2e5f4ff0f2b90b03cfee557777f5d3df706873e74b366fa34d9aa051L10)
[[2]](diffhunk://#diff-e43b091c2e5f4ff0f2b90b03cfee557777f5d3df706873e74b366fa34d9aa051R130)

Tree type model updates:

* Removed the `photo_id` field and the associated `photo` method from
the `TreeType` model.
[[1]](diffhunk://#diff-9b6e2b512620cbcf6691b6ebed684af80c6d3071e2078483f32bff09693632d9L13-L14)
[[2]](diffhunk://#diff-9b6e2b512620cbcf6691b6ebed684af80c6d3071e2078483f32bff09693632d9L27-L36)

Tree type view updates:

* Updated the back button URL in the tree type creation and edit views
to point to the correct admin route.
[[1]](diffhunk://#diff-44d3d8172b626b0986e1ec03c382c4a1ea15ff0c7ae3df656290f9e83bb2126eL2-R2)
[[2]](diffhunk://#diff-78a2b54262dc0a49ba0f676ccd46806c3483d7f6a76d921b3edc39e50f240536L2-R2)
* Added an `id` attribute to the tree type creation and edit forms and
included an error message container for validation feedback.
[[1]](diffhunk://#diff-44d3d8172b626b0986e1ec03c382c4a1ea15ff0c7ae3df656290f9e83bb2126eL14-R17)
[[2]](diffhunk://#diff-78a2b54262dc0a49ba0f676ccd46806c3483d7f6a76d921b3edc39e50f240536L14-R17)

Client-side validation:

* Added a new JavaScript function `validateFormTreeType` in `app.js` to
perform client-side validation for the tree type form fields and display
error messages if validation fails.
  • Loading branch information
0x1026 authored Dec 9, 2024
2 parents 2206ec4 + 03cbb61 commit fa8c202
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 17 deletions.
4 changes: 2 additions & 2 deletions app/src/app/Layouts/Admin/AdminLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<title>
<?php echo $title . ' - ' . getenv('APP_NAME'); ?>
</title>
<script src="/assets/js/app.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="/assets/css/app.css">
</head>
Expand Down Expand Up @@ -128,6 +127,7 @@ class="bg-gray-700 hover:bg-gray-600 text-white font-medium py-2 px-4 rounded-lg
</div>
</div>

<script src="/assets/js/app.js"></script>
<!-- Javascript, add class d-none to alert-msg after 5 seconds if it exists -->
<script>
setTimeout(() => {
Expand All @@ -140,4 +140,4 @@ class="bg-gray-700 hover:bg-gray-600 text-white font-medium py-2 px-4 rounded-lg

</body>

</html>
</html>
9 changes: 0 additions & 9 deletions app/src/app/Models/TreeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class TreeType extends BaseModel

public string $species;

public ?int $photo_id;

protected static function getTableName(): string
{
return 'tree_types';
Expand All @@ -24,14 +22,7 @@ protected static function mapDataToModel($data): TreeType
$tree_type->family = $data['family'];
$tree_type->genus = $data['genus'];
$tree_type->species = $data['species'];
$tree_type->photo_id = $data['photo_id'];
$tree_type->created_at = $data['created_at'];

return $tree_type;
}

public function photo(): ?Photo
{
return $this->photo_id ? $this->belongsTo(Photo::class, 'photo_id') : null;
}
}
7 changes: 5 additions & 2 deletions app/src/app/Views/Admin/TreeType/Create.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="mb-4 flex justify-end">
<a href="/tree-types/"
<a href="/admin/tree-types"
class="bg-blue-500 hover:bg-blue-600 text-white font-medium py-2 px-4 rounded-lg shadow focus:outline-none focus:ring focus:ring-green-500 flex items-center space-x-2">
<!-- Heroicon for return/back (chevron-left) -->
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
Expand All @@ -11,7 +11,10 @@ class="bg-blue-500 hover:bg-blue-600 text-white font-medium py-2 px-4 rounded-lg

<div class="bg-white p-8 border border-gray-300 rounded-lg shadow-md">
<h2 class="text-2xl font-semibold text-gray-800 mb-6">Create Tree Type</h2>
<form action="/admin/tree-type/store" method="POST" class="space-y-6">
<form id="treeForm" action="/admin/tree-type/store" method="POST" class="space-y-6">
<div id="errorMessages"
class="hidden bg-red-100 border border-red-400 text-red700 px-4 py-3 rounded relative mb-6"></div>
<!-- Family -->
<div>
Expand Down
8 changes: 5 additions & 3 deletions app/src/app/Views/Admin/TreeType/Edit.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="mb-4 flex justify-end">
<a href="/tree-types"
<a href="/admin/tree-types"
class="bg-blue-500 hover:bg-blue-600 text-white font-medium py-2 px-4 rounded-lg shadow focus:outline-none focus:ring focus:ring-green-500 flex items-center space-x-2">
<!-- Heroicon for return/back (chevron-left) -->
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
Expand All @@ -11,8 +11,10 @@ class="bg-blue-500 hover:bg-blue-600 text-white font-medium py-2 px-4 rounded-lg

<div class="bg-white p-8 border border-gray-300 rounded-lg shadow-md">
<h2 class="text-2xl font-semibold text-gray-800 mb-6">Edit Tree Type</h2>
<form action="/admin/tree-type/<?php echo htmlspecialchars($tree_type->getId()); ?>/update" method="POST"
class="space-y-6">
<form id="treeForm" action="/admin/tree-type/<?php echo htmlspecialchars($tree_type->getId()); ?>/update"
method="POST" class="space-y-6">
<div id="errorMessages"
class="hidden bg-red-100 border border-red-400 text-red700 px-4 py-3 rounded relative mb-6"></div>

<!-- Family -->
<div>
Expand Down
2 changes: 1 addition & 1 deletion app/src/app/Views/Admin/TreeTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ class="text-red-500 hover:text-red-700" title="Delete">
<?php } ?>
</tbody>
</table>
</div>
</div>
58 changes: 58 additions & 0 deletions app/src/public/assets/js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
function validateFormTreeType(event) {
// Contenidor per a tots els errors
const errorMessagesDiv = document.getElementById("errorMessages");

// Netejar errors anteriors
errorMessagesDiv.innerHTML = ""; // Eliminar contingut d'errors
errorMessagesDiv.classList.add("hidden"); // Amagar inicialment

let hasError = false; // Controlar si hi ha errors
let errorMessage = ""; // Acumular missatges d'error

// Expressió regular per validar noms (només lletres i espais)
const namePattern = /^[a-zA-Z\s]+$/;

// Validació de cada camp
const family = document.getElementById("family").value.trim();
const genus = document.getElementById("genus").value.trim();
const species = document.getElementById("species").value.trim();

if (!family) {
errorMessage += "<p>- La família és obligatòria.</p>";
hasError = true;
} else if (!namePattern.test(family)) {
errorMessage +=
"<p>- La família només pot contenir lletres i espais.</p>";
hasError = true;
}

if (!genus) {
errorMessage += "<p>- El gènere és obligatori.</p>";
hasError = true;
} else if (!namePattern.test(genus)) {
errorMessage +=
"<p>- El gènere només pot contenir lletres i espais.</p>";
hasError = true;
}

if (!species) {
errorMessage += "<p>- L'espècie és obligatòria.</p>";
hasError = true;
} else if (!namePattern.test(species)) {
errorMessage +=
"<p>- L'espècie només pot contenir lletres i espais.</p>";
hasError = true;
}

// Si hi ha errors, mostrar el contenidor i prevenir l'enviament del formulari
if (hasError) {
errorMessagesDiv.innerHTML = errorMessage; // Inserir errors al quadre
errorMessagesDiv.classList.remove("hidden"); // Mostrar el contenidor
event.preventDefault(); // Evitar l'enviament del formulari
}
}

// Assignar l'esdeveniment submit al formulari
document
.getElementById("treeForm")
.addEventListener("submit", validateFormTreeType);

0 comments on commit fa8c202

Please sign in to comment.