Skip to content

Commit 6fb84a9

Browse files
committedJun 10, 2024
Validation for the new user form is complete. Users are now pushed into database.
1 parent 18014ea commit 6fb84a9

File tree

8 files changed

+79
-22
lines changed

8 files changed

+79
-22
lines changed
 

‎App/controllers/UserController.php

+57-2
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,71 @@ public function store(){
4444
$values[$name] = $value;
4545
};
4646

47+
48+
//---------------------------------------------------------------------------------FORM VALIDATION
49+
4750
$errors = [];
4851
if (!Validation::validateEmail($values["email"])){
4952
$errors["email"] = "Please enter valid email adress.";
5053
}
54+
if (!Validation::validateString($values["name"], 2, 50)){
55+
$errors["name"] = "Name must be between 2 and 50 characters long.";
56+
}
57+
if (!Validation::validateString($values["password"], 6, 50)){
58+
$errors["password"] = "Password must be between 6 and 50 characters long.";
59+
}
60+
if (!Validation::compareStrings($values["password"], $values["password_confirmation"])){
61+
$errors["parrword_confirmation"] = "Passwords do not match.";
62+
}
63+
64+
65+
//---------------------------------------------------------------------------------REDIRECT IF ERRORS PRESENT
5166

5267
if (!empty($errors)){
53-
loadView("users/register/register", $user = $values);
68+
loadView("users/register/register", [
69+
"user" => $values,
70+
"errors" => $errors,
71+
]);
5472
exit;
5573
};
56-
inspectValueANdHold("Stored");
5774

75+
//---------------------------------------------------------------------------------DB QUERY TO CHECK IF EMAIL ALREDY EXISTS
76+
77+
$dbParams = [
78+
'email' => $values['email'],
79+
];
80+
81+
$query = "SELECT * FROM users WHERE email = :email";
82+
$user = $this->database->query($query, $dbParams)->fetchAll();
83+
84+
if ($user){
85+
$errors['email'] = "User with that email adress alredy exists.";
86+
loadView("users/register/register", [
87+
"user" => $values,
88+
"errors" => $errors,
89+
]);
90+
exit;
91+
};
92+
93+
//---------------------------------------------------------------------------------PUSH NEW USER INTO DB
94+
95+
$dbParams = [];
96+
foreach ($values as $name => $value) {
97+
if ($name === 'password'){
98+
$dbParams[$name] = password_hash($value, PASSWORD_DEFAULT);
99+
} else if ($name !== 'password_confirmation'){
100+
$dbParams[$name] = $value;
101+
};
102+
};
103+
104+
$query = "INSERT INTO users (email, name, password, city, state) VALUES (:email, :name, :password, :city, :state)";
105+
$this->database->query($query, $dbParams);
106+
107+
108+
//--------------------------------------------------------------------------------REDIRECT NEW USER
109+
110+
redirect("/");
111+
112+
58113
}
59114
}

‎App/views/listings/create/create.php

+3-9
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,13 @@
1010
<section class="flex justify-center items-center mt-20">
1111
<div class="bg-white p-8 rounded-lg shadow-md w-full md:w-600 mx-6">
1212
<h2 class="text-4xl text-center font-bold mb-4">Create Job Listing</h2>
13-
<!-- <div class="message bg-red-100 p-3 my-3">This is an error message.</div>
14-
<div class="message bg-green-100 p-3 my-3">
15-
This is a success message.
16-
</div> -->
1713
<form method="POST" action="/listings">
1814
<h2 class="text-2xl font-bold mb-6 text-center text-gray-500">
1915
Job Info
2016
</h2>
21-
<?php if (isset($errors)) : ?>
22-
<?php foreach ($errors as $error) : ?>
23-
<div class="message bg-red-100 my-3"><?= $error ?></div>
24-
<?php endforeach; ?>
25-
<?php endif; ?>
17+
<?php loadPartial("errors", [
18+
'errors' => $errors ??= [],
19+
]) ?>
2620
<div class="mb-4">
2721
<input
2822
type="text"

‎App/views/listings/edit/edit.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020
<h2 class="text-2xl font-bold mb-6 text-center text-gray-500">
2121
Job Info
2222
</h2>
23-
<?php if (isset($errors)) : ?>
24-
<?php foreach ($errors as $error) : ?>
25-
<div class="message bg-red-100 my-3"><?= $error ?></div>
26-
<?php endforeach; ?>
27-
<?php endif; ?>
23+
<?php loadPartial("errors", [
24+
'errors' => $errors ??= [],
25+
]) ?>
2826
<div class="mb-4">
2927
<input
3028
type="text"

‎App/views/partials/errors.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php if (isset($errors)) : ?>
2+
<?php foreach ($errors as $error) : ?>
3+
<div class="message bg-red-100 my-3"><?= $error ?></div>
4+
<?php endforeach; ?>
5+
<?php endif; ?>

‎App/views/users/login/login.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
<div class="flex justify-center items-center mt-20">
77
<div class="bg-white p-8 rounded-lg shadow-md w-full md:w-500 mx-6">
88
<h2 class="text-4xl text-center font-bold mb-4">Login</h2>
9-
<!-- <div class="message bg-red-100 p-3 my-3">This is an error message.</div>
10-
<div class="message bg-green-100 p-3 my-3">
11-
This is a success message.
12-
</div> -->
9+
<?php loadPartial("errors") ?>
1310
<form>
1411
<div class="mb-4">
1512
<input

‎App/views/users/register/register.php

+7
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
<div class="flex justify-center items-center mt-20">
77
<div class="bg-white p-8 rounded-lg shadow-md w-full md:w-500 mx-6">
88
<h2 class="text-4xl text-center font-bold mb-4">Register</h2>
9+
<?php loadPartial("errors", [
10+
'errors' => $errors ??= [],
11+
]) ?>
912
<form method="POST" action="/register">
1013
<div class="mb-4">
1114
<input
1215
type="text"
1316
name="name"
1417
placeholder="Full Name"
1518
class="w-full px-4 py-2 border rounded focus:outline-none"
19+
value = "<?= $user["name"] ??= '' ?>"
1620
/>
1721
</div>
1822
<div class="mb-4">
@@ -21,6 +25,7 @@ class="w-full px-4 py-2 border rounded focus:outline-none"
2125
name="email"
2226
placeholder="Email Address"
2327
class="w-full px-4 py-2 border rounded focus:outline-none"
28+
value = "<?= $user["email"] ??= '' ?>"
2429
/>
2530
</div>
2631
<div class="mb-4">
@@ -29,6 +34,7 @@ class="w-full px-4 py-2 border rounded focus:outline-none"
2934
name="city"
3035
placeholder="City"
3136
class="w-full px-4 py-2 border rounded focus:outline-none"
37+
value = "<?= $user["city"] ??= '' ?>"
3238
/>
3339
</div>
3440
<div class="mb-4">
@@ -37,6 +43,7 @@ class="w-full px-4 py-2 border rounded focus:outline-none"
3743
name="state"
3844
placeholder="State"
3945
class="w-full px-4 py-2 border rounded focus:outline-none"
46+
value = "<?= $user["state"] ??= '' ?>"
4047
/>
4148
</div>
4249
<div class="mb-4">

‎framework/Validation.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static function validateEmail($email){
3333
return filter_var($email, FILTER_VALIDATE_EMAIL);
3434
}
3535

36-
//------------------------------------------------------------------------------
36+
//------------------------------------------------------------------------------STRING COMPARISON
3737

3838
/**
3939
* Method compares two strings, returns true if they are identical

‎helpers.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ function loadView($name, $data = []) {
3939
* @return void
4040
*/
4141

42-
function loadPartial($name) {
42+
function loadPartial($name, $data = []) {
4343

4444
$partialPath = basePath("App/views/partials/{$name}.php");
4545

4646
if (file_exists($partialPath)) {
47+
extract($data);
4748
require $partialPath;
4849
}else{
4950
echo "ERROR: Specified partial path: {$partialPath} does not exist";

0 commit comments

Comments
 (0)
Please sign in to comment.