2014-02-10 00:40:20 +01:00
|
|
|
<?php
|
2017-08-09 01:55:09 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\UserForms\Model\EditableFormField;
|
|
|
|
|
|
|
|
use SilverStripe\Forms\FieldGroup;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Forms\LiteralField;
|
|
|
|
use SilverStripe\Forms\NumericField;
|
|
|
|
use SilverStripe\UserForms\Model\EditableFormField;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2014-02-10 00:40:20 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
{
|
2014-02-10 00:40:20 +01:00
|
|
|
|
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
|
|
|
|
2017-04-28 10:31:51 +02:00
|
|
|
private static $has_placeholder = true;
|
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $db = [
|
2016-07-21 07:53:59 +02:00
|
|
|
'MinValue' => 'Int',
|
2017-04-28 10:31:51 +02:00
|
|
|
'MaxValue' => 'Int'
|
2017-08-11 01:33:06 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
private static $table_name = 'EditableNumericField';
|
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
|
|
|
/**
|
|
|
|
* @return NumericField
|
|
|
|
*/
|
|
|
|
public function getFormField()
|
|
|
|
{
|
|
|
|
$field = NumericField::create($this->Name, $this->EscapedTitle, $this->Default)
|
2017-08-14 02:29:57 +02:00
|
|
|
->setFieldHolderTemplate(EditableFormField::class . '_holder')
|
|
|
|
->setTemplate(EditableFormField::class)
|
2016-07-21 07:53:59 +02:00
|
|
|
->addExtraClass('number');
|
2015-08-21 05:21:33 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->doUpdateFormField($field);
|
2015-08-21 05:21:33 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
return $field;
|
|
|
|
}
|
2014-07-24 11:37:11 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function getFieldValidationOptions()
|
|
|
|
{
|
|
|
|
$fields = parent::getFieldValidationOptions();
|
|
|
|
$fields->push(FieldGroup::create(
|
2017-08-11 01:33:06 +02:00
|
|
|
_t(__CLASS__.'.RANGE', 'Allowed numeric range'),
|
|
|
|
[
|
|
|
|
NumericField::create('MinValue', false),
|
|
|
|
LiteralField::create('RangeValue', _t(__CLASS__.'.RANGE_TO', 'to')),
|
|
|
|
NumericField::create('MaxValue', false)
|
|
|
|
]
|
2016-07-21 07:53:59 +02:00
|
|
|
));
|
|
|
|
return $fields;
|
|
|
|
}
|
2014-07-24 11:37:11 +02:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 00:40:20 +01:00
|
|
|
}
|