|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Alvo\User\HTMLForm; |
|
4
|
|
|
|
|
5
|
|
|
use \Anax\HTMLForm\FormModel; |
|
6
|
|
|
use \Anax\DI\DIInterface; |
|
7
|
|
|
use \Alvo\User\User; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Example of FormModel implementation. |
|
11
|
|
|
*/ |
|
12
|
|
|
class CreateUserForm extends FormModel |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Constructor injects with DI container. |
|
16
|
|
|
* |
|
17
|
|
|
* @param Anax\DI\DIInterface $di a service container |
|
|
|
|
|
|
18
|
|
|
*/ |
|
19
|
1 |
|
public function __construct(DIInterface $di) |
|
20
|
|
|
{ |
|
21
|
1 |
|
parent::__construct($di); |
|
22
|
|
|
|
|
23
|
1 |
|
$this->form->create( |
|
24
|
|
|
[ |
|
25
|
1 |
|
"id" => __CLASS__, |
|
26
|
|
|
"legend" => "Create user", |
|
27
|
|
|
"class" => "center form", |
|
28
|
|
|
"wrapper-element" => "div", |
|
29
|
|
|
"use_fieldset" => false, |
|
30
|
|
|
// "br-after-label" => false, |
|
31
|
|
|
], |
|
32
|
|
|
[ |
|
33
|
1 |
|
"email" => [ |
|
34
|
1 |
|
"type" => "email", |
|
35
|
|
|
"validation" => [ |
|
36
|
|
|
"custom_test" => [ |
|
37
|
1 |
|
"message" => "User with this email is already registered", |
|
38
|
1 |
View Code Duplication |
"test" => function ($email) { |
|
|
|
|
|
|
39
|
|
|
$user = new User(); |
|
40
|
|
|
$user->setDb($this->di->get("db")); |
|
41
|
|
|
$check = $user->find('email', $email); |
|
42
|
|
|
return !$check; |
|
43
|
1 |
|
} |
|
44
|
|
|
], |
|
45
|
1 |
|
"not_empty" |
|
46
|
|
|
] |
|
47
|
|
|
], |
|
48
|
|
|
|
|
49
|
|
|
"password" => [ |
|
50
|
|
|
"type" => "password", |
|
51
|
|
|
"validation" => [ |
|
52
|
|
|
"not_empty" |
|
53
|
|
|
], |
|
54
|
|
|
], |
|
55
|
|
|
|
|
56
|
|
|
"password-again" => [ |
|
57
|
|
|
"type" => "password", |
|
58
|
|
|
"validation" => [ |
|
59
|
|
|
"match" => "password" |
|
60
|
|
|
], |
|
61
|
|
|
], |
|
62
|
|
|
|
|
63
|
|
|
"submit" => [ |
|
64
|
1 |
|
"type" => "submit", |
|
65
|
1 |
|
"value" => "Create user", |
|
66
|
1 |
|
"callback" => [$this, "callbackSubmit"], |
|
67
|
1 |
|
"class" => "btn btn-success" |
|
68
|
|
|
], |
|
69
|
|
|
] |
|
70
|
|
|
); |
|
71
|
1 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Callback for submit-button which should return true if it could |
|
77
|
|
|
* carry out its work and false if something failed. |
|
78
|
|
|
* |
|
79
|
|
|
* @return boolean true if okey, false if something went wrong. |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function callbackSubmit() |
|
82
|
|
|
{ |
|
83
|
|
|
// Get values from the submitted form |
|
84
|
1 |
|
$email = $this->form->value("email"); |
|
85
|
1 |
|
$password = $this->form->value("password"); |
|
86
|
1 |
|
$passwordAgain = $this->form->value("password-again"); |
|
87
|
|
|
|
|
88
|
|
|
// Check password matches |
|
89
|
1 |
|
if ($password !== $passwordAgain) { |
|
90
|
|
|
$this->form->rememberValues(); |
|
91
|
|
|
$this->form->addOutput("Password did not match."); |
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
if (!$email || !$password || !$passwordAgain) { |
|
96
|
1 |
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$user = new User(); |
|
100
|
|
|
$user->setDb($this->di->get("db")); |
|
101
|
|
|
$user->email = $email; |
|
102
|
|
|
$user->setPassword($password); |
|
103
|
|
|
$user->created = date("Y-m-d H:i:s"); |
|
104
|
|
|
$user->save(); |
|
105
|
|
|
|
|
106
|
|
|
$this->form->addOutput("User was created."); |
|
107
|
|
|
|
|
108
|
|
|
return true; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|