Completed
Push — master ( ed2b3e...68d467 )
by Mikael
02:34
created

FormModelValidateNumber::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 25
nc 1
nop 1
dl 0
loc 39
ccs 0
cts 28
cp 0
crap 2
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Anax\HTMLForm;
4
5
use Anax\DI\DIInterface;
6
7
/**
8
 * Example of FormModel implementation.
9
 */
10
class FormModelValidateNumber extends FormModel
11
{
12
    /**
13
     * Constructor injects with DI container.
14
     *
15
     * @param Anax\DI\DIInterface $di a service container
16
     */
17
    public function __construct(DIInterface $di)
18
    {
19
        parent::__construct($di);
20
        $this->form->create(
21
            [
22
                "id" => __CLASS__,
23
                "legend" => "Legend",
24
            ],
25
            [
26
                "num1" => [
27
                    "type" =>"text",
28
                    "label" => "Numeric (as input type=text)",
29
                    "validation" => ["number"],
30
                ],
31
32
                "num2" => [
33
                    "type" =>"number",
34
                    "label" => "Numeric (as input type=number)",
35
                    "validation" => ["number"],
36
                ],
37
38
                "num3" => [
39
                    "type" =>"range",
40
                    "label" => "Numeric (as input type=range)",
41
                    "validation" => ["number"],
42
                    "value"       => 42,
43
                    "min"         => 0,
44
                    "max"         => 100,
45
                    "step"        => 2,
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
        $this->form->addOutput("Validation passes.");
68
69
        // Remember values during resubmit, for sake of the example
70
        $this->form->rememberValues();
71
72
        return true;
73
    }
74
}
75