Skip to content

Commit 9f66d61

Browse files
committed
extract a form validation object
1 parent d50b668 commit 9f66d61

20 files changed

+54
-27
lines changed

.DS_Store

6 KB
Binary file not shown.

Core/router.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function route($uri, $method)
6969
if ($route['uri'] === $uri && $route['method'] === strtoupper($method)) {
7070
Middleware::resolve($route['middleware']);
7171

72-
return require base_path($route['controller']);
72+
return require base_path('Http/controllers/' . $route['controller']);
7373
}
7474
}
7575
}

Http/.DS_Store

6 KB
Binary file not shown.

Http/Forms/LoginForm.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Http\Forms;
4+
5+
use Core\Validator;
6+
7+
class LoginForm
8+
{
9+
protected $errors = [];
10+
public function validate($email, $password)
11+
12+
{
13+
14+
15+
if (!Validator::email($email)) {
16+
$this->errors['email'] = 'Please provide a valid email address.';
17+
}
18+
19+
if (!Validator::string($password, 7, 255)) {
20+
$this->errors['password'] = 'Please provide a valid password.';
21+
}
22+
23+
24+
return empty($this->errors);
25+
}
26+
27+
public function errors()
28+
{
29+
30+
return $this->errors;
31+
}
32+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

controllers/session/store.php Http/controllers/session/store.php

+6-11
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,26 @@
33
use Core\App;
44
use Core\Database;
55
use Core\Validator;
6+
use Http\Forms\LoginForm;
67

78
$db = App::resolve(Database::class);
89

910
$email = $_POST['email'];
1011

1112
$password = $_POST['password'];
1213

13-
$errors = [];
1414

15-
if (!Validator::email($email)) {
16-
$errors['email'] = 'Please provide a valid email address.';
17-
}
18-
19-
if (!Validator::string($password, 7, 255)) {
20-
$errors['password'] = 'Please provide a password of at least seven characters.';
21-
}
15+
$form = new LoginForm();
2216

23-
24-
if (!empty($errors)) {
17+
if (!$form->validate($email, $password)) {
2518

2619
return view('session/create.view.php', [
27-
'errors' => $errors
20+
21+
'errors' => $form->errors()
2822
]);
2923
}
3024

25+
3126
$user = $db->query('select * from users where email = :email', [
3227
'email' => $email
3328
])->find();

routes.php

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
<?php
22

33

4-
$router->get('/', 'controllers/index.php');
4+
$router->get('/', 'index.php');
55

6-
$router->get('/about', 'controllers/about.php');
6+
$router->get('/about', 'about.php');
77

8-
$router->get('/contact', 'controllers/contact.php');
8+
$router->get('/contact', 'contact.php');
99

10-
$router->get('/notes', 'controllers/notes/index.php')->only('auth');
10+
$router->get('/notes', 'notes/index.php')->only('auth');
1111

12-
$router->get('/notes', 'controllers/notes/show.php');
12+
$router->get('/notes', 'notes/show.php');
1313

14-
$router->delete('/note', 'controllers/notes/destroy.php');
14+
$router->delete('/note', 'notes/destroy.php');
1515

16-
$router->get('/note/edit', 'controllers/notes/edit.php');
16+
$router->get('/note/edit', 'notes/edit.php');
1717

18-
$router->patch('/notes', 'controllers/notes/update.php');
18+
$router->patch('/notes', 'notes/update.php');
1919

20-
$router->get('/notes/create', 'controllers/notes/create.php');
20+
$router->get('/notes/create', 'notes/create.php');
2121

22-
$router->post('/notes', 'controllers/notes/store.php');
22+
$router->post('/notes', 'notes/store.php');
2323

24-
$router->get('/register', 'controllers/registration/create.php')->only('guest');
24+
$router->get('/register', 'registration/create.php')->only('guest');
2525

26-
$router->post('/register', 'controllers/registration/store.php')->only('guest');
26+
$router->post('/register', 'registration/store.php')->only('guest');
2727

28-
$router->get('/login', 'controllers/session/create.php')->only('guest');
28+
$router->get('/login', 'session/create.php')->only('guest');
2929

30-
$router->post('/session', 'controllers/session/store.php')->only('guest');
30+
$router->post('/session', 'session/store.php')->only('guest');
3131

32-
$router->delete('/session', 'controllers/session/destroy.php')->only('auth');
32+
$router->delete('/session', 'session/destroy.php')->only('auth');

0 commit comments

Comments
 (0)