From 739d6166ca5ce89d91483ca77eaf14b63d16805d Mon Sep 17 00:00:00 2001
From: filippo.bertilotti <filippobertilotti@gmail.com>
Date: Wed, 22 May 2024 11:25:15 +0200
Subject: [PATCH] correzione e visualizzazione errore direttamente tramite form (parte 129)

---
 src/App/Controllers/products.php |   14 +++++++++++---
 views/Products/new.php           |    4 ++++
 src/App/Database.php             |    2 --
 3 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/src/App/Controllers/products.php b/src/App/Controllers/products.php
index 0165711..bf0a304 100644
--- a/src/App/Controllers/products.php
+++ b/src/App/Controllers/products.php
@@ -43,8 +43,16 @@
             "description" => empty($_POST["description"]) ? null : $_POST["description"]
         ];
         
-        var_dump($this->model->insert($data));
-
-        print_r($this->model->getErrors());
+        if($this->model->insert($data)) {
+            echo "record saved";
+        } else {
+            echo $this->viewer->render("shared/header.php", [
+                "title" => "New Product"
+            ]);
+            
+            echo $this->viewer->render("Products/new.php", 
+                ["errors" => $this->model->getErrors()
+            ]);
+        };
     }
 }
\ No newline at end of file
diff --git a/src/App/Database.php b/src/App/Database.php
index 45ca5dc..a239bdb 100644
--- a/src/App/Database.php
+++ b/src/App/Database.php
@@ -10,8 +10,6 @@
                                 private string $name,
                                 private string $user,
                                 private string $password) {
-    
-    echo "Created Database";
 
     }
     public function getConnection(): PDO {
diff --git a/views/Products/new.php b/views/Products/new.php
index 5e34cfa..4de2e4a 100644
--- a/views/Products/new.php
+++ b/views/Products/new.php
@@ -4,6 +4,10 @@
         <label for="name">Name</label>
         <input type="text", id="name", name="name">
 
+        <?php if (isset($errors["name"])): ?>
+            <p><?= $errors["name"] ?></p>
+        <?php endif; ?>
+
         <label for="description">Description</label>
         <textarea id="description", name="description"></textarea>
 

--
Gitblit v1.8.0