|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Anax\HTMLForm; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Anax base class for wrapping sessions. |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
class CFormExample extends \Mos\HTMLForm\CForm |
|
10
|
|
|
{ |
|
11
|
|
|
use \Anax\DI\TInjectionaware, |
|
12
|
|
|
\Anax\MVC\TRedirectHelpers; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Constructor |
|
18
|
|
|
* |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct() |
|
21
|
|
|
{ |
|
22
|
|
|
parent::__construct([], [ |
|
23
|
|
|
'name' => [ |
|
24
|
|
|
'type' => 'text', |
|
25
|
|
|
'label' => 'Name of contact person:', |
|
26
|
|
|
'required' => true, |
|
27
|
|
|
'validation' => ['not_empty'], |
|
28
|
|
|
], |
|
29
|
|
|
'email' => [ |
|
30
|
|
|
'type' => 'text', |
|
31
|
|
|
'required' => true, |
|
32
|
|
|
'validation' => ['not_empty', 'email_adress'], |
|
33
|
|
|
], |
|
34
|
|
|
'phone' => [ |
|
35
|
|
|
'type' => 'text', |
|
36
|
|
|
'required' => true, |
|
37
|
|
|
'validation' => ['not_empty', 'numeric'], |
|
38
|
|
|
], |
|
39
|
|
|
'submit' => [ |
|
40
|
|
|
'type' => 'submit', |
|
41
|
|
|
'callback' => [$this, 'callbackSubmit'], |
|
42
|
|
|
], |
|
43
|
|
|
'submit-fail' => [ |
|
44
|
|
|
'type' => 'submit', |
|
45
|
|
|
'callback' => [$this, 'callbackSubmitFail'], |
|
46
|
|
|
], |
|
47
|
|
|
]); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Customise the check() method. |
|
54
|
|
|
* |
|
55
|
|
|
* @param callable $callIfSuccess handler to call if function returns true. |
|
56
|
|
|
* @param callable $callIfFail handler to call if function returns true. |
|
57
|
|
|
*/ |
|
58
|
|
|
public function check($callIfSuccess = null, $callIfFail = null) |
|
59
|
|
|
{ |
|
60
|
|
|
return parent::check([$this, 'callbackSuccess'], [$this, 'callbackFail']); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Callback for submit-button. |
|
67
|
|
|
* |
|
68
|
|
|
*/ |
|
69
|
|
|
public function callbackSubmit() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->AddOutput("<p><i>DoSubmit(): Form was submitted. Do stuff (save to database) and return true (success) or false (failed processing form)</i></p>"); |
|
72
|
|
|
$this->AddOutput("<p><b>Name: " . $this->Value('name') . "</b></p>"); |
|
73
|
|
|
$this->AddOutput("<p><b>Email: " . $this->Value('email') . "</b></p>"); |
|
74
|
|
|
$this->AddOutput("<p><b>Phone: " . $this->Value('phone') . "</b></p>"); |
|
75
|
|
|
$this->saveInSession = true; |
|
76
|
|
|
return true; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Callback for submit-button. |
|
83
|
|
|
* |
|
84
|
|
|
*/ |
|
85
|
|
|
public function callbackSubmitFail() |
|
86
|
|
|
{ |
|
87
|
|
|
$this->AddOutput("<p><i>DoSubmitFail(): Form was submitted but I failed to process/save/validate it</i></p>"); |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Callback What to do if the form was submitted? |
|
95
|
|
|
* |
|
96
|
|
|
*/ |
|
97
|
|
|
public function callbackSuccess() |
|
98
|
|
|
{ |
|
99
|
|
|
$this->AddOUtput("<p><i>Form was submitted and the callback method returned true.</i></p>"); |
|
100
|
|
|
$this->redirectTo(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Callback What to do when form could not be processed? |
|
107
|
|
|
* |
|
108
|
|
|
*/ |
|
109
|
|
|
public function callbackFail() |
|
110
|
|
|
{ |
|
111
|
|
|
$this->AddOutput("<p><i>Form was submitted and the Check() method returned false.</i></p>"); |
|
112
|
|
|
$this->redirectTo(); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|