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

94 lines
2.3 KiB
PHP
Raw Normal View History

<?php
/**
* EditableNumericField
*
* This control represents a user-defined numeric field in a user defined form
*
* @package userforms
*/
2016-07-21 07:53:59 +02:00
class EditableNumericField extends EditableFormField
{
2016-07-21 07:53:59 +02:00
private static $singular_name = 'Numeric Field';
2015-09-11 00:20:06 +02:00
2016-07-21 07:53:59 +02:00
private static $plural_name = 'Numeric Fields';
2015-07-24 04:37:48 +02:00
2016-07-21 07:53:59 +02:00
private static $db = array(
'MinValue' => 'Int',
'MaxValue' => 'Int',
'Placeholder' => 'Varchar(255)'
);
2015-09-11 00:20:06 +02:00
2016-07-21 07:53:59 +02:00
public function getSetsOwnError()
{
return true;
}
2015-09-11 00:20:06 +02:00
2016-07-21 07:53:59 +02:00
public function getCMSFields()
{
$this->beforeUpdateCMSFields(function ($fields) {
$fields->addFieldToTab(
'Root.Main',
TextField::create(
'Placeholder',
_t('EditableTextField.PLACEHOLDER', 'Placeholder')
)
);
});
2016-07-21 07:53:59 +02:00
return parent::getCMSFields();
}
2016-07-21 07:53:59 +02:00
/**
* @return NumericField
*/
public function getFormField()
{
$field = NumericField::create($this->Name, $this->EscapedTitle, $this->Default)
->setFieldHolderTemplate('UserFormsField_holder')
->setTemplate('UserFormsField')
->addExtraClass('number');
2016-07-21 07:53:59 +02:00
$this->doUpdateFormField($field);
2016-07-21 07:53:59 +02:00
return $field;
}
2016-07-21 07:53:59 +02:00
public function getFieldValidationOptions()
{
$fields = parent::getFieldValidationOptions();
$fields->push(FieldGroup::create(
_t("EditableNumericField.RANGE", "Allowed numeric range"),
array(
new NumericField('MinValue', false),
new LiteralField('RangeValue', _t("EditableNumericField.RANGE_TO", "to")),
new NumericField('MaxValue', false)
)
));
return $fields;
}
2016-07-21 07:53:59 +02:00
/**
* Updates a formfield with the additional metadata specified by this field
*
* @param FormField $field
*/
protected function updateFormField($field)
{
parent::updateFormField($field);
2015-08-11 01:40:37 +02:00
2016-07-21 07:53:59 +02:00
if ($this->MinValue) {
$field->setAttribute('data-rule-min', $this->MinValue);
}
2015-08-11 01:40:37 +02:00
2016-07-21 07:53:59 +02:00
if ($this->MaxValue) {
$field->setAttribute('data-rule-max', $this->MaxValue);
}
2016-07-21 07:53:59 +02:00
if ($this->Placeholder) {
$field->setAttribute('placeholder', $this->Placeholder);
}
}
}