Version: current

     1 <?php
     2 /**
     3  * Single checkbox field.
     4  * @package forms
     5  * @subpackage fields-basic
     6  */
     7 class CheckboxField extends FormField {
     8
     9     function setValue($value) {
    10         $this->value = ($value) ? 1 : 0;
    11     }
    12
    13     function dataValue() {
    14         return ($this->value) ? 1 : 0;
    15     }
    16
    17     function Value() {
    18         return ($this->value) ? 1 : 0;
    19     }
    20
    21     function Field() {
    22         $attributes = array(
    23             'type' => 'checkbox',
    24             'class' => ($this->extraClass() ? $this->extraClass() : ''),
    25             'id' => $this->id(),
    26             'name' => $this->Name(),
    27             'value' => 1,
    28             'checked' => $this->value ? 'checked' : '',
    29             'tabindex' => $this->getTabIndex()
    30         );
    31         return $this->createTag('input', $attributes);
    32     }
    33
    34     function useLabelLeft( $labelLeft = true ) {
    35         $this->labelLeft = $labelLeft;
    36     }
    37
    38     /**
    39      * Returns a readonly version of this field
    40      */
    41     function performReadonlyTransformation() {
    42         $field = new CheckboxField_Readonly($this->name, $this->title, $this->value );
    43         $field->setForm($this->form);
    44         return $field;
    45     }
    46
    47     function performDisabledTransformation() {
    48         $clone = clone $this;
    49         $clone->setDisabled(true);
    50         return $clone;
    51     }
    52 }
    53
    54 /**
    55  * Readonly version of a checkbox field - "Yes" or "No".
    56  * @package forms
    57  * @subpackage fields-basic
    58  */
    59 class CheckboxField_Readonly extends ReadonlyField {
    60     function performReadonlyTransformation() {
    61         return clone $this;
    62     }
    63
    64     function setValue($val) {
    65         $this->value = ($val) ? _t('CheckboxField.YES', 'Yes') : _t('CheckboxField.NO', 'No');
    66     }
    67 }
    68
    69 /**
    70  * Single checkbox field, disabled
    71  * @package forms
    72  * @subpackage fields-basic
    73  */
    74 class CheckboxField_Disabled extends CheckboxField {
    75
    76     function __construct($name, $title = null, $value = null, $form = null, $rightTitle = null) {
    77         $this->disabled = true;
    78
    79         parent::__construct($name, $title = null, $value = null, $form = null, $rightTitle = null);
    80     }
    81
    82     /**
    83      * Returns a single checkbox field - used by templates.
    84      */
    85     function Field() {
    86         $attributes = array(
    87             'type' => 'checkbox',
    88             'class' => 'text' . ($this->extraClass() ? $this->extraClass() : ''),
    89             'id' => $this->id(),
    90             'name' => $this->Name(),
    91             'tabindex' => $this->getTabIndex(),
    92             'checked' => ($this->value) ? 'checked' : false,
    93         );
    94         return $this->createTag('input', $attributes);
    95     }
    96 }
    97 ?>

[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. -