From ef902284397f50707335acb50fd5dd55365165b3 Mon Sep 17 00:00:00 2001 From: Patryk Hegenberg Date: Tue, 20 Dec 2022 08:29:19 +0100 Subject: [PATCH] Part on Form validation finished --- controllers/note-create.php | 20 +++++++++++++++++--- views/note-create.view.php | 17 +++++++++++++++-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/controllers/note-create.php b/controllers/note-create.php index 5fd31da..7691083 100644 --- a/controllers/note-create.php +++ b/controllers/note-create.php @@ -5,7 +5,21 @@ $password = 'password'; $config = require('config.php'); $db = new Database($config['database'], $username, $password); -if($_SERVER['REQUEST_METHOD'] === 'POST'){ - $db->query("INSERT INTO notes (body, user_id) VALUES (:body, :user_id)", ['body' => $_POST['body'], 'user_id' => 1]); +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $errors = []; + + if (strlen($_POST['body'] === 0)) { + $errors['body'] = 'A body is required.'; + } + if (strlen($_POST['body'] > 1000)) { + $errors['body'] = 'The body cann not be more than 1000 characters.'; + } + if (empty($errors)) { + + $db->query("INSERT INTO notes (body, user_id) VALUES (:body, :user_id)", [ + 'body' => $_POST['body'], + 'user_id' => 1 + ]); + } } -require 'views/note-create.view.php'; \ No newline at end of file +require 'views/note-create.view.php'; diff --git a/views/note-create.view.php b/views/note-create.view.php index 075abf7..8ed397f 100644 --- a/views/note-create.view.php +++ b/views/note-create.view.php @@ -13,12 +13,25 @@
- + + +

+
- +