silverstripe-framework/Forms/OptionsetField.php

151 lines
3.7 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\Forms;
use SilverStripe\Core\Convert;
use SilverStripe\ORM\ArrayList;
use SilverStripe\View\ArrayData;
/**
* Set of radio buttons designed to emulate a dropdown.
2014-08-15 08:53:05 +02:00
*
* This field allows you to ensure that a form element is submitted is not optional and is part of a fixed set of
* data. This field uses the input type of radio. It's a direct subclass of {@link DropdownField},
* so the constructor and arguments are in the same format.
2014-08-15 08:53:05 +02:00
*
* <b>Usage</b>
2014-08-15 08:53:05 +02:00
*
* <code>
* new OptionsetField(
* $name = "Foobar",
* $title = "FooBar's optionset",
* $source = array(
* "1" => "Option 1",
* "2" => "Option 2",
* "3" => "Option 3",
* "4" => "Option 4",
* "5" => "Option 5"
* ),
* $value = "1"
* );
* </code>
2014-08-15 08:53:05 +02:00
*
* You can use the helper functions on data object set to create the source array. eg:
*
* <code>
* //Database request for the object
* $map = FooBar::get()->map();
2016-09-09 08:43:05 +02:00
* // returns a Map object containing an array of ID => Title
*
2014-08-15 08:53:05 +02:00
* // Instantiate the OptionsetField
* $FieldList = new FieldList(
* new OptionsetField(
* $name = "Foobar",
* $title = "FooBar's optionset",
* $source = $map,
* $value = $map[0]
* )
* );
*
* // Pass the fields to the form constructor. etc
* </code>
2014-08-15 08:53:05 +02:00
*
* @see CheckboxSetField for multiple selections through checkboxes instead.
* @see DropdownField for a simple <select> field with a single element.
* @see TreeDropdownField for a rich and customizeable UI that can visualize a tree of selectable elements
*/
class OptionsetField extends SingleSelectField {
protected $schemaComponent = 'OptionsetField';
2014-08-15 08:53:05 +02:00
/**
* Build a field option for template rendering
*
* @param mixed $value Value of the option
* @param string $title Title of the option
* @param boolean $odd True if this should be striped odd. Otherwise it should be striped even
* @return ArrayData Field option
*/
protected function getFieldOption($value, $title, $odd) {
return new ArrayData(array(
'ID' => $this->getOptionID($value),
'Class' => $this->getOptionClass($value, $odd),
'Name' => $this->getOptionName(),
'Value' => $value,
'Title' => $title,
'isChecked' => $this->isSelectedValue($value, $this->Value()),
'isDisabled' => $this->isDisabledValue($value)
));
}
/**
* Generate an ID property for a single option
*
* @param string $value
* @return string
*/
protected function getOptionID($value) {
return $this->ID() . '_' . Convert::raw2htmlid($value);
}
/**
* Get the "name" property for each item in the list
*
* @return string
*/
protected function getOptionName() {
return $this->getName();
}
2014-08-15 08:53:05 +02:00
/**
* Get extra classes for each item in the list
*
* @param string $value Value of this item
* @param bool $odd If this item is odd numbered in the list
* @return string
*/
protected function getOptionClass($value, $odd) {
$oddClass = $odd ? 'odd' : 'even';
$valueClass = ' val' . Convert::raw2htmlid($value);
return $oddClass . $valueClass;
}
public function Field($properties = array()) {
$options = array();
$odd = false;
2016-03-08 21:50:18 +01:00
// Add all options striped
foreach($this->getSourceEmpty() as $value => $title) {
$odd = !$odd;
$options[] = $this->getFieldOption($value, $title, $odd);
}
$properties = array_merge($properties, array(
'Options' => new ArrayList($options)
));
return FormField::Field($properties);
switch externals to trunk. Inform-merge: from the changeset: r32477: Merge 2.0-inform from trunk previously r32478: Merge 2.0-inform from trunk previously r32481: merge 2.0infom with lastes chunk r32483: merge 2.0infom with lastes chunk r33526: Final styling of all forms and combined communication form add/alter javascript for height adjustment of First / Second block r33580: styling for combined form communication in myinfom pages r33706: styling of combined form (communication) in Email r33881: made compatible to $extraClass r33885: added defaultVal r33887: fixed typo r34728: modified SmallFieldHolder?() r34729: added "validationError"-class r34914: WIP3866: Factfinder: Hide "self emplyed" block r34964: Change current plan upto TraumaInsurance? r35038: disabled friggin field focus r35230: #1032 Fixed hash-link insertion r35887: conditionally setting parameters in sourceID() - to avoid empty overrides r35892: Saving value in SQL-compatible format (YYYY-MM-DD instead of DD/MM/YYYY), with fallback for non-sql values (just passed through without conversion) r35928: Removed "create a" from PageType?-dropdown, sorting alphabetically, falling back to $singular_name r35990: branched off for membertablefield r35994: fix for membertablefield r36024: added array-condition needed for DMYDateField r36083: fix bug for compositeField -> dropDatalessField r36394: removed debug code r36826: change wrong indent format r36828: WIP 4262: Logging out of My Inform goes to blank page r36858: Fixed error caused in r12472 while merging to Session-class r37132: Merged partial changesets from branches/2.0-nzct, only adding childID to detailform when not in add-mode r40815: add an unsubscribe record when a member subscribe a newslettertype r41113: fix the bug described in http://support.silverstripe.com/info/ticket/31: CRM not showing search results r43226: fixed search (partial merge from trunk) r43268: merged createNewPassword() from trunk, was referencing a non-existinent global function randomString() git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@45473 467b73ca-7a2a-4603-9d3b-597d59a354a9
2007-11-23 02:10:19 +01:00
}
/**
* {@inheritdoc}
*/
public function validate($validator) {
if (!$this->Value()) {
return true;
}
return parent::validate($validator);
}
public function getAttributes() {
$attributes = parent::getAttributes();
unset($attributes['name']);
unset($attributes['required']);
unset($attributes['role']);
return $attributes;
}
}