CFormExample   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
c 3
b 0
f 0
lcom 1
cbo 3
dl 0
loc 107
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 29 1
A check() 0 4 1
A callbackSubmitFail() 0 5 1
A callbackSuccess() 0 5 1
A callbackFail() 0 5 1
A callbackSubmit() 0 10 1
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>DoSubmit(): Form was submitted.<p>");
72
        $this->AddOutput("<p>Do stuff (save to database) and return true (success) or false (failed processing)</p>");
73
        $this->AddOutput("<p><b>Name: " . $this->Value('name') . "</b></p>");
74
        $this->AddOutput("<p><b>Email: " . $this->Value('email') . "</b></p>");
75
        $this->AddOutput("<p><b>Phone: " . $this->Value('phone') . "</b></p>");
76
        $this->saveInSession = true;
0 ignored issues
show
Bug introduced by
The property saveInSession does not seem to exist. Did you mean session?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
77
        return true;
78
    }
79
80
81
82
    /**
83
     * Callback for submit-button.
84
     *
85
     */
86
    public function callbackSubmitFail()
87
    {
88
        $this->AddOutput("<p><i>DoSubmitFail(): Form was submitted but I failed to process/save/validate it</i></p>");
89
        return false;
90
    }
91
92
93
94
    /**
95
     * Callback What to do if the form was submitted?
96
     *
97
     */
98
    public function callbackSuccess()
99
    {
100
        $this->AddOUtput("<p><i>Form was submitted and the callback method returned true.</i></p>");
101
        $this->redirectTo();
102
    }
103
104
105
106
    /**
107
     * Callback What to do when form could not be processed?
108
     *
109
     */
110
    public function callbackFail()
111
    {
112
        $this->AddOutput("<p><i>Form was submitted and the Check() method returned false.</i></p>");
113
        $this->redirectTo();
114
    }
115
}
116