Test Failed
Push — master ( 194522...eda1b6 )
by Mikael
01:43
created

FormModelCheckboxMultiple::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
3
namespace Anax\HTMLForm;
4
5
/**
6
 * Example of FormModel implementation.
7
 */
8
class FormModelCheckboxMultiple extends FormModel
9
{
10
    /**
11
     * Constructor
12
     */
13
    public function __construct()
14
    {
15
        parent::__construct(
16
            [
17
                "id" => __CLASS__,
18
            ],
19
            [
20
                "items" => [
21
                    "type"        => "checkbox-multiple",
22
                    "values"      => ["tomato", "potato", "apple", "pear", "banana"],
23
                    "checked"     => ["potato", "pear"],
24
                ],
25
                "submit" => [
26
                    "type"      => "submit",
27
                    "callback"  => [$this, "callbackSubmit"],
28
                ],
29
                "submit-fail" => [
30
                    "type"      => "submit",
31
                    "callback"  => [$this, "callbackSubmitFail"],
32
                ],
33
            ]
34
        );
35
    }
36
37
38
39
    /**
40
     * Callback for submit-button.
41
     */
42 View Code Duplication
    public function callbackSubmit()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44
        $this->AddOutput("<p>#callbackSubmit()</p>");
45
        
46
        // Get the selected items as an array
47
        $items = $this->value("items");
48
        $itemsAsString = implode(", ", $items);
49
        $this->AddOutput("<p>Selected items are: '$itemsAsString'.");
50
51
        $this->AddOutput("<pre>" . print_r($_POST, 1) . "</pre>");
52
        $this->AddOutput("<pre>" . print_r($this["items"], 1) . "</pre>");
53
        $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...
54
        
55
        return true;
56
    }
57
58
59
60
    /**
61
     * Callback for submit-button.
62
     */
63
    public function callbackSubmitFail()
64
    {
65
        $this->AddOutput("<p>#callbackSubmitFail()</p>");
66
        $this->AddOutput("<p>Form was submitted but I failed to process/save/validate it</p>");
67
        return false;
68
    }
69
}
70