Passed
Push — 4.0.4 ( fe4f6f )
by Robbie
09:31
created

PasswordField::getAllowValuePostback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Forms;
4
5
use SilverStripe\Dev\Deprecation;
6
7
/**
8
 * Password input field.
9
 */
10
class PasswordField extends TextField
11
{
12
    /**
13
     * Controls the autocomplete attribute on the field.
14
     *
15
     * Setting it to false will set the attribute to "off", which will hint the browser
16
     * to not cache the password and to not use any password managers.
17
     */
18
    private static $autocomplete;
0 ignored issues
show
introduced by
The private property $autocomplete is not used, and could be removed.
Loading history...
19
20
    protected $inputType = 'password';
21
22
    /**
23
     * If true, the field can accept a value attribute, e.g. from posted form data
24
     * @var bool
25
     */
26
    protected $allowValuePostback = false;
27
28
    /**
29
     * Returns an input field.
30
     *
31
     * @param string $name
32
     * @param null|string $title
33
     * @param string $value
34
     */
35
    public function __construct($name, $title = null, $value = '')
36
    {
37
        if (count(func_get_args()) > 3) {
38
            Deprecation::notice(
39
                '3.0',
40
                'Use setMaxLength() instead of constructor arguments',
41
                Deprecation::SCOPE_GLOBAL
42
            );
43
        }
44
45
        parent::__construct($name, $title, $value);
46
    }
47
48
    /**
49
     * @param bool $bool
50
     * @return $this
51
     */
52
    public function setAllowValuePostback($bool)
53
    {
54
        $this->allowValuePostback = (bool) $bool;
55
56
        return $this;
57
    }
58
59
    /**
60
     * @return bool
61
     */
62
    public function getAllowValuePostback()
63
    {
64
        return $this->allowValuePostback;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getAttributes()
71
    {
72
        $attributes = [];
73
74
        if (!$this->getAllowValuePostback()) {
75
            $attributes['value'] = null;
76
        }
77
78
        $autocomplete = $this->config()->get('autocomplete');
79
80
        if ($autocomplete) {
81
            $attributes['autocomplete'] = 'on';
82
        } else {
83
            $attributes['autocomplete'] = 'off';
84
        }
85
86
        return array_merge(
87
            parent::getAttributes(),
88
            $attributes
89
        );
90
    }
91
92
    /**
93
     * Creates a read-only version of the field.
94
     *
95
     * @return FormField
96
     */
97
    public function performReadonlyTransformation()
98
    {
99
        $field = $this->castedCopy('SilverStripe\\Forms\\ReadonlyField');
100
101
        $field->setValue('*****');
102
103
        return $field;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function Type()
110
    {
111
        return 'text password';
112
    }
113
}
114