Completed
Push — 3 ( 4a9e99...f688bc )
by Robbie
11:41
created

PasswordField::setAllowValuePostback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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