silverstripe-userforms/code/Model/EditableFormField/EditableRadioField.php

63 lines
1.7 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\UserForms\Model\EditableFormField;
use SilverStripe\Forms\FieldList;
use SilverStripe\UserForms\FormField\UserFormsOptionSetField;
use SilverStripe\UserForms\Model\EditableCustomRule;
/**
* EditableRadioField
*
* Represents a set of selectable radio buttons
*
* @package userforms
*/
2016-07-21 07:53:59 +02:00
class EditableRadioField extends EditableMultipleOptionField
{
private static $singular_name = 'Radio Group';
private static $plural_name = 'Radio Groups';
private static $table_name = 'EditableRadioField';
2016-07-21 07:53:59 +02:00
/**
* @return FieldList
*/
public function getCMSFields()
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$fields->removeByName('Default');
});
2016-07-21 07:53:59 +02:00
return parent::getCMSFields();
2016-07-21 07:53:59 +02:00
}
public function getFormField()
{
$field = UserFormsOptionSetField::create($this->Name, $this->Title ?: false, $this->getOptionsMap())
->setFieldHolderTemplate(EditableMultipleOptionField::class . '_holder')
->setTemplate(UserFormsOptionSetField::class);
2016-07-21 07:53:59 +02:00
// Set default item
$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)
{
// We only want to trigger on load once for the radio group - hence we focus on the first option only.
$first = $forOnLoad ? ':first' : '';
return "$(\"input[name='{$this->Name}']{$first}\")";
}
2017-08-11 02:37:03 +02:00
public function isRadioField()
{
return true;
}
}