KnockoutConfirmedPasswordField   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 56
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 3
1
<?php
2
namespace AntonyThorpe\Knockout;
3
4
require_once('Common.php');
5
require_once('CommonComposite.php');
6
use SilverStripe\Forms\ConfirmedPasswordField;
7
8
/**
9
 * Creates a {@link ConfirmedPasswordField} with an additional data-bind attribute that links to a Knockout observable
10
 * @uses 'confirmedPassword' as the default observable
11
 */
12
class KnockoutConfirmedPasswordField extends ConfirmedPasswordField
13
{
14
    use \AntonyThorpe\Knockout\Common;
15
    use \AntonyThorpe\Knockout\CommonComposite;
16
17
    /**
18
     * bindingType
19
     *
20
     * KnockoutConfirmedPasswordField needs either 'value' or 'textInput' as a key for the 'data-bind' HTML attribute
21
     *
22
     * @var string data-bind attribute key
23
     * @example  data-bind="input: name, valueUpdate: 'input'" - the binding type is: input.
24
     */
25
    protected $bindingType = "textInput";
26
27
    /**
28
     * casting of variables for security purposes
29
     *
30
     * @see http://docs.silverstripe.org/en/3.1/developer_guides/security/secure_coding/
31
     */
32
    protected $casting = array(
33
        "Observable" => "Varchar",
34
        "Observables" => "Array",
35
        "BindingType" => "Varchar",
36
        "OtherBindings" => "Varchar",
37
        "HasFocus" => "Boolean"
38
    );
39
40
    /**
41
     * Constructor
42
     *
43
     * @param string $name
44
     * @param null|string $title
45
     * @param string $value
46
     */
47
    public function __construct($name, $title = null, $value = "", $form = null, $showOnClick = false, $titleConfirmField = null)
48
    {
49
        parent::__construct($name, $title, $value, $form, $showOnClick, $titleConfirmField);
50
51
        // swap fields for the knockout ones
52
        foreach ($this->children as $key => $field) {
53
            $knockout_field = KnockoutPasswordField::create(
54
                $field->getName(),
55
                $field->Title(),
56
                $field->Value()
57
            );
58
            $this->children->replaceField(
59
                $field->getName(),
60
                $knockout_field
61
            );
62
            if ($key == 1) {
63
                $knockout_field->setObservable('confirmedPassword');
64
            }
65
        }
66
67
        $this->setFieldHolderTemplate('AntonyThorpe/Knockout/KnockoutFormField_holder');
68
    }
69
}
70