FormModelCheckbox::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 1
dl 0
loc 35
ccs 0
cts 11
cp 0
crap 2
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
namespace Anax\HTMLForm;
4
5
use Psr\Container\ContainerInterface;
6
7
/**
8
 * Example of FormModel implementation.
9
 */
10
class FormModelCheckbox extends FormModel
11
{
12
    /**
13
     * Constructor injects with DI container.
14
     *
15
     * @param Anax\DI\DIInterface $di a service container
0 ignored issues
show
Bug introduced by
The type Anax\HTMLForm\Anax\DI\DIInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
     */
17
    public function __construct(ContainerInterface $di)
18
    {
19
        parent::__construct($di);
20
21
        $license = "You must accept the <a href=http://opensource.org/licenses/GPL-3.0>license agreement</a>.";
22
        
23
        $this->form->create(
24
            [
25
                "id" => __CLASS__,
26
                "legend" => "Legend"
27
            ],
28
            [
29
                "accept_mail" => [
30
                    "type"      => "checkbox",
31
                    "label"     => "It´s great if you send me product information by mail.",
32
                    "checked"   => false,
33
                ],
34
35
                "accept_phone" => [
36
                    "type"      => "checkbox",
37
                    "label"     => "You may call me to try and sell stuff.",
38
                    "checked"   => true,
39
                ],
40
41
                "accept_agreement" => [
42
                    "type"        => "checkbox",
43
                    "label"       => $license,
44
                    "required"    => true,
45
                    "validation"  => ["must_accept"],
46
                ],
47
48
                "submit" => [
49
                    "type" => "submit",
50
                    "value" => "Submit",
51
                    "callback" => [$this, "callbackSubmit"]
52
                ],
53
            ]
54
        );
55
    }
56
57
58
59
    /**
60
     * Callback for submit-button which should return true if it could
61
     * carry out its work and false if something failed.
62
     *
63
     * @return boolean true if okey, false if something went wrong.
64
     */
65
    public function callbackSubmit()
66
    {
67
        $elements = ["accept_mail", "accept_phone", "accept_agreement"];
68
        foreach ($elements as $name) {
69
            $this->form->addOutput(
70
                "$name is "
71
                . ($this->form->value($name)
72
                    ? ""
73
                    : "not ")
74
                . "checked</br>"
75
            );
76
        }
77
78
        // Remember values during resubmit, for sake of the example
79
        $this->form->rememberValues();
80
81
        return true;
82
    }
83
}
84