-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
57 lines (49 loc) · 1.62 KB
/
example.php
File metadata and controls
57 lines (49 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
// alias the most used classes
use MintyPHP\Form\Elements as E;
use MintyPHP\Form\Validator\Validators as V;
// ensure all classes are (auto)loaded:
require_once 'vendor/autoload.php';
// set style to Bulma
E::$style = 'bulma';
// create a form object
$form = E::form([
E::field(E::text('username')->required(), E::label('Username'), [V::minLength(1)]),
E::field(E::password('password'), E::label('Password')),
E::field(E::submit('Login')),
]);
// check if the form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// fill the form with the submitted data
$form->fill($_POST);
// if the form is valid, process the data
if ($form->validate()) {
// extract the data
$data = $form->extract();
// process the data (e.g., login, save to database, etc.)
if ($data['username'] == 'user' && $data['password'] == 'pass') {
// redirect to the dashboard page
die('logged in, redirecting to dashboard...');
} else {
// invalidate credentials
$form->addErrors([
'username' => 'Invalid username/password combination',
'password' => 'Invalid username/password combination',
]);
}
}
} else {
// if the form is not submitted, fill it with empty values
$form->fill(['username' => '', 'password' => '']);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/1.0.3/css/bulma.min.css">
</head>
<body class="container p-5">
<h1 class="title">Login</h1>
<?php $form->render(); ?>
</body>
</html>