2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\Forms;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Single checkbox field.
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
class CheckboxField extends FormField
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_BOOLEAN;
|
2017-01-26 05:20:08 +01:00
|
|
|
|
|
|
|
public function setValue($value, $data = null)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
$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()
|
|
|
|
{
|
2017-05-19 05:47:51 +02:00
|
|
|
$attributes = parent::getAttributes();
|
|
|
|
$attributes['value'] = 1;
|
|
|
|
if ($this->Required()) {
|
|
|
|
// Semantically, it doesn't make sense to have a required attribute
|
|
|
|
// on a field in which both checked and unchecked are allowable.
|
|
|
|
unset($attributes['aria-required']);
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
return array_merge(
|
2017-05-19 05:47:51 +02:00
|
|
|
$attributes,
|
2016-11-29 00:31:16 +01:00
|
|
|
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;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|