Passed
Branch master (e5ff15)
by Antony
01:18
created

KnockoutConfirmedPasswordField::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 3
nop 6
dl 0
loc 33
rs 9.6666
c 0
b 0
f 0
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 obervable
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,
48
            $titleConfirmField = null)
49
    {
50
        parent::__construct($name, $title, $value, $form, $showOnClick, $titleConfirmField);
51
52
        // Variables
53
54
        $fields = $this->children;
55
        $password_field = $fields->fieldByName('{$name}[_Password]');
0 ignored issues
show
Unused Code introduced by
The assignment to $password_field is dead and can be removed.
Loading history...
56
        $confirmed_password_field = $fields->fieldByName('{$name}[_ConfirmPassword]');
0 ignored issues
show
Unused Code introduced by
The assignment to $confirmed_password_field is dead and can be removed.
Loading history...
57
58
        // swap fields for the knockout ones
59
        foreach($fields as $key=>$field) {
60
            $name = $field->getName();
61
            $title = $field->Title();
62
            $value = $field->Value();
63
            $knockout_field = KnockoutPasswordField::create(
64
                $name,
65
                $title,
66
                $value
67
            );
68
69
            $fields->replaceField(
70
                $name,
71
                $knockout_field
72
            );
73
74
            if($key == 1) {
75
                $knockout_field->setObservable('confirmedPassword');
76
            }
77
        }
78
79
        $this->setFieldHolderTemplate('KnockoutFormField_holder');
80
81
    }
82
}
83