silverstripe-userforms/code/model/editableformfields/EditableDropdown.php

76 lines
1.8 KiB
PHP
Raw Normal View History

<?php
/**
* EditableDropdown
*
* Represents a modifiable dropdown (select) box on a form
*
* @property bool $UseEmptyString
* @property string $EmptyString
*
* @package userforms
*/
2016-07-21 07:53:59 +02:00
class EditableDropdown extends EditableMultipleOptionField
{
private static $singular_name = 'Dropdown Field';
private static $plural_name = 'Dropdowns';
private static $db = array(
'UseEmptyString' => 'Boolean',
'EmptyString' => 'Varchar(255)',
);
2016-07-21 07:53:59 +02:00
/**
* @return FieldList
*/
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab(
'Root.Main',
CheckboxField::create('UseEmptyString')
->setTitle('Set default empty string')
);
$fields->addFieldToTab(
'Root.Main',
TextField::create('EmptyString')
->setTitle('Empty String')
);
2016-07-21 07:53:59 +02:00
$fields->removeByName('Default');
return $fields;
}
/**
* @return DropdownField
*/
public function getFormField()
{
$field = DropdownField::create($this->Name, $this->EscapedTitle, $this->getOptionsMap())
->setFieldHolderTemplate('UserFormsField_holder')
->setTemplate('UserFormsDropdownField');
if ($this->UseEmptyString) {
$field->setEmptyString(($this->EmptyString) ? $this->EmptyString : '');
}
2016-07-21 07:53:59 +02:00
// Set default
$defaultOption = $this->getDefaultOptions()->first();
if ($defaultOption) {
2017-06-13 01:49:02 +02:00
$field->setValue($defaultOption->Value);
2016-07-21 07:53:59 +02:00
}
$this->doUpdateFormField($field);
return $field;
}
public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false)
{
return "$(\"select[name='{$this->Name}']\")";
}
}