Part on Form validation finished

This commit is contained in:
Patryk Hegenberg 2022-12-20 08:29:19 +01:00
parent a597943b45
commit ef90228439
2 changed files with 32 additions and 5 deletions

View file

@ -6,6 +6,20 @@ $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]);
$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';

View file

@ -13,12 +13,25 @@
<div>
<label for="body" class="block text-sm font-medium text-gray-700">Body</label>
<div class="mt-1">
<textarea id="body" name="body" rows="3" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm"></textarea>
<textarea
id="body"
name="body"
rows="3"
class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm"
placeholder="Here is an idea for a note."
//required
><?= $_POST['body'] ?? '' ?>
</textarea>
<?php if (isset($errors['body'])) : ?>
<p class="text-red-500 text-xs mt-2"><?= $erros['body'] ?></p>
<?php endif; ?>
</div>
</div>
</div>
<div class="bg-gray-50 px-4 py-3 text-right sm:px-6">
<button type="submit" class="inline-flex justify-center rounded-md border border-transparent bg-indigo-600 py-2 px-4 text-sm font-medium text-white shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2">Save</button>
<button
type="submit"
class="inline-flex justify-center rounded-md border border-transparent bg-indigo-600 py-2 px-4 text-sm font-medium text-white shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2">Save</button>
</div>
</div>
</form>