silverstripe-framework/forms/CheckboxField.php
Ingo Schommer 9e98f127f2 Merged revisions 47613 via svnmerge from
svn://svn.silverstripe.com/silverstripe/modules/sapphire/branches/2.2.0-mesq

........
  r47613 | ischommer | 2008-01-04 19:14:58 +1300 (Fri, 04 Jan 2008) | 1 line
  
  adding tabIndex capabilities, switching HTML-rendering to createTag()
........


git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@52199 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-04-06 04:08:45 +00:00

110 lines
2.4 KiB
PHP
Executable File

<?php
/**
* @package forms
* @subpackage fields-basic
*/
/**
* Single checkbox field.
* @package forms
* @subpackage fields-basic
*/
class CheckboxField extends FormField {
/**
* Returns a single checkbox field - used by templates.
*
* Shouldn't this have a value?
*/
protected $disabled;
function Field() {
$attributes = array(
'type' => 'checkbox',
'class' => $this->extraClass(),
'id' => $this->id(),
'name' => $this->attrName(),
'value' => 1,
'tabindex' => $this->getTabIndexHTML(),
'checked' => $this->value ? "checked=\"checked\"" : '',
'disabled' => $this->disabled ? " disabled=\"disabled\"" : "",
);
return $this->createTag('input', $attributes);
}
function dataValue() {
return $this->value ? 1 : 0;
}
/**
* Checkboxes use the RightLabelledFieldHolder template, to put the field on the left
* and the label on the right. See {@link FormField::FieldHolder} for more information about
* how FieldHolder works.
*/
function FieldHolder() {
if($this->labelLeft) {
return parent::FieldHolder();
} else {
extract($this->getXMLValues(array( 'Name', 'Field', 'Title', 'Message', 'MessageType' )));
$messageBlock = isset($Message) ? "<span class=\"message $MessageType\">$Message</span>" : '';
$Type = $this->XML_val('Type');
return <<<HTML
<p id="$Name" class="field $Type">
$Field
<label class="right" for="{$this->id()}">$Title</label>
$messageBlock
</p>
HTML;
}
}
function useLabelLeft( $labelLeft = true ) {
$this->labelLeft = $labelLeft;
}
/**
* Returns a restricted field holder used within things like FieldGroups
*/
function SmallFieldHolder() {
$result = $this->Field();
if($t = $this->Title()) {
$result .= "<label for=\"" . $this->id() ."\">$t</label> ";
}
return $result;
}
/**
* Returns a readonly version of this field
*/
function performReadonlyTransformation() {
$field = new CheckboxField_Readonly($this->name, $this->title, $this->value ? 'Yes' : 'No');
$field->setForm($this->form);
return $field;
}
function performDisabledTransformation() {
$this->disabled = true;
return $this;
}
}
/**
* Readonly version of a checkbox field - "Yes" or "No".
* @package forms
* @subpackage fields-basic
*/
class CheckboxField_Readonly extends ReadonlyField {
function performReadonlyTransformation() {
return $this;
}
function setValue($val) {
$this->value = ($val) ? 'Yes' : 'No';
}
}
?>