silverstripe-framework/forms/CheckboxField.php
Ingo Schommer 559abecd56 API Copying instance props on FormField readonly/disabled transformations
Introduced new FormField->castedCopy() method
which tries to replicate the existing form field instance
as closely as possible.

Primarily, the fix was targeted at consistently passing
through FormField->description to all of its variations.
2012-12-14 01:58:04 +01:00

63 lines
1.2 KiB
PHP

<?php
/**
* Single checkbox field.
*
* @package forms
* @subpackage fields-basic
*/
class CheckboxField extends FormField {
public function setValue($value) {
$this->value = ($value) ? 1 : 0;
return $this;
}
public function dataValue() {
return ($this->value) ? 1 : NULL;
}
public function Value() {
return ($this->value) ? 1 : 0;
}
public function getAttributes() {
$attrs = parent::getAttributes();
$attrs['value'] = 1;
return array_merge(
$attrs,
array(
'checked' => ($this->Value()) ? 'checked' : null,
'type' => 'checkbox',
)
);
}
/**
* Returns a readonly version of this field
*/
public function performReadonlyTransformation() {
$field = new CheckboxField_Readonly($this->name, $this->title, $this->value);
$field->setForm($this->form);
return $field;
}
}
/**
* Readonly version of a checkbox field - "Yes" or "No".
*
* @package forms
* @subpackage fields-basic
*/
class CheckboxField_Readonly extends ReadonlyField {
public function performReadonlyTransformation() {
return clone $this;
}
public function Value() {
return Convert::raw2xml($this->value ? _t('CheckboxField.YES', 'Yes') : _t('CheckboxField.NO', 'No'));
}
}