1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 class NullableField extends FormField {
22 23 24 25
26 protected $valueField;
27
28 29 30 31
32 protected $isNullLabel;
33
34
35 36 37 38 39
40 function __construct(FormField $valueField, $isNullLabel = null) {
41 $this->valueField = $valueField;
42 $this->isNullLabel = $isNullLabel;
43 if ( is_null($this->isNullLabel) ) {
44
45 $this->isNullLabel = _t('NullableField.IsNullLabel', 'Is Null', PR_HIGH);
46 }
47 parent::__construct($valueField->Name(), $valueField->Title(), $valueField->Value(), $valueField->getForm(), $valueField->RightTitle());
48 $this->readonly = $valueField->isReadonly();
49 }
50
51 52 53 54
55 function getIsNullLabel() {
56 return $this->isNullLabel;
57 }
58 59 60 61
62 function setIsNullLabel(string $isNulLabel){
63 $this->isNullLabel = $isNulLabel;
64 }
65
66 67 68 69
70 function getIsNullId() {
71 return $this->Name() . "_IsNull";
72 }
73
74 75 76 77
78 function Field() {
79 if ( $this->isReadonly()) {
80 $nullableCheckbox = new CheckboxField_Readonly($this->getIsNullId());
81 } else {
82 $nullableCheckbox = new CheckboxField($this->getIsNullId());
83 }
84 $nullableCheckbox->setValue(is_null($this->dataValue()));
85 return $this->valueField->Field() . ' ' . $nullableCheckbox->Field() . ' <span>' . $this->getIsNullLabel().'</span>';
86 }
87
88 89 90
91 function setValue($value, $data = null) {
92 if ( is_array($data) && array_key_exists($this->getIsNullId(), $data) && $data[$this->getIsNullId()] ) {
93 $value = null;
94 }
95 $this->valueField->setValue($value);
96 parent::setValue($value);
97 }
98
99 100 101 102
103 function setName($name) {
104
105 $this->valueField->setName($name);
106 parent::setName($name);
107 }
108
109 110 111 112
113 function debug() {
114 $result = "$this->class ($this->name: $this->title : <font style='color:red;'>$this->message</font>) = ";
115 $result .= (is_null($this->value)) ? "<<null>>" : $this->value;
116 return result;
117 }
118 }
119
[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.
-