silverstripe-framework/forms/CheckboxField.php
Damian Mooyman 5c9044a007 API Enforce default_cast for all field usages
API Introduce HTMLFragment as casting helper for HTMLText with shortcodes disabled
API Introduce DBField::CDATA for XML file value encoding
API RSSFeed now casts from the underlying model rather than by override
API Introduce CustomMethods::getExtraMethodConfig() to allow metadata to be queried
BUG Remove _call hack from VirtualPage
API Remove FormField::$dontEscape
API Introduce HTMLReadonlyField for non-editable readonly HTML
API FormField::Field() now returns string in many cases rather than DBField instance.
API Remove redundant *_val methods from ViewableData
API ViewableData::obj() no longer has a $forceReturnObject parameter as it always returns an object
BUG  Fix issue with ViewableData caching incorrect field values after being modified.
API Remove deprecated DB class methods
API Enforce plain text left/right formfield titles
2016-07-13 17:15:45 +12:00

71 lines
1.3 KiB
PHP

<?php
/**
* Single checkbox field.
*
* @package forms
* @subpackage fields-basic
*/
class CheckboxField extends FormField {
protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_SINGLESELECT;
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 $this->value ?
_t('CheckboxField.YESANSWER', 'Yes') :
_t('CheckboxField.NOANSWER', 'No');
}
public function getValueCast() {
return 'Text';
}
}