1 <?php
2 3 4 5 6
7 class CheckboxField extends FormField {
8
9 protected $disabled;
10
11 function setValue($value) {
12 $this->value = ($value) ? 1 : 0;
13 }
14
15 function dataValue() {
16 return ($this->value) ? 1 : 0;
17 }
18
19 function Value() {
20 return ($this->value) ? 1 : 0;
21 }
22
23 function Field() {
24 $attributes = array(
25 'type' => 'checkbox',
26 'class' => ($this->extraClass() ? $this->extraClass() : ''),
27 'id' => $this->id(),
28 'name' => $this->Name(),
29 'value' => 1,
30 'checked' => $this->value ? 'checked' : '',
31 'tabindex' => $this->getTabIndex()
32 );
33
34 if($this->disabled) $attributes['disabled'] = 'disabled';
35
36 return $this->createTag('input', $attributes);
37 }
38
39 40 41 42 43
44 function FieldHolder() {
45 if($this->labelLeft) {
46 return parent::FieldHolder();
47 } else {
48 extract($this->getXMLValues(array( 'Name', 'Field', 'Title', 'Message', 'MessageType' )),
49 EXTR_SKIP);
50 $messageBlock = isset($Message) ? "<span class=\"message $MessageType\">$Message</span>" : '';
51 $Type = $this->XML_val('Type');
52 $extraClass = $this->XML_val('extraClass');
53 $RightBlock = ($this->RightTitle()) ? '<span class="right">'.$this->XML_val('RightTitle').'</span>' : '';
54 return <<<HTML
55 <p id="$Name" class="field $Type $extraClass">
56 <span class="middleColumn middlecolumn">$Field
57 <label class="right" for="{$this->id()}">$Title</label></span>
58 $RightBlock
59 $messageBlock
60 </p>
61 HTML;
62
63 }
64 }
65
66 function useLabelLeft( $labelLeft = true ) {
67 $this->labelLeft = $labelLeft;
68 }
69
70 71 72
73 function SmallFieldHolder() {
74 $result = $this->Field();
75 if($t = $this->Title()) {
76 $result .= "<label for=\"" . $this->id() ."\">$t</label> ";
77 }
78 return $result;
79 }
80
81 82 83
84
85 function performReadonlyTransformation() {
86 $field = new CheckboxField_Readonly($this->name, $this->title, $this->value ? _t('CheckboxField.YES', 'Yes') : _t('CheckboxField.NO', 'No'));
87 $field->setForm($this->form);
88 return $field;
89 }
90
91 function performDisabledTransformation() {
92 $clone = clone $this;
93 $clone->setDisabled(true);
94 return $clone;
95 }
96 }
97
98 99 100 101 102
103 class CheckboxField_Readonly extends ReadonlyField {
104 function performReadonlyTransformation() {
105 return clone $this;
106 }
107
108 function setValue($val) {
109 $this->value = (int)($val) ? _t('CheckboxField.YES', 'Yes') : _t('CheckboxField.NO', 'No');
110 }
111 }
112
113 114 115 116 117
118 class CheckboxField_Disabled extends CheckboxField {
119
120 protected $disabled = true;
121
122 123 124
125 function Field() {
126 $attributes = array(
127 'type' => 'checkbox',
128 'class' => 'text' . ($this->extraClass() ? $this->extraClass() : ''),
129 'id' => $this->id(),
130 'name' => $this->Name(),
131 'tabindex' => $this->getTabIndex(),
132 'checked' => ($this->value) ? 'checked' : false,
133 'disabled' => 'disabled'
134 );
135
136 return $this->createTag('input', $attributes);
137 }
138 }
139 ?>
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-