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;
|
2021-02-26 04:13:23 +01:00
|
|
|
use SilverStripe\Forms\FormField;
|
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
|
2021-02-26 04:13:23 +01:00
|
|
|
* @property int $MaxValue
|
|
|
|
* @property int $MinValue
|
2014-02-10 00:40:20 +01:00
|
|
|
*/
|
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()
|
|
|
|
{
|
2018-03-23 04:23:51 +01:00
|
|
|
$field = NumericField::create($this->Name, $this->Title ?: false, $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);
|
|
|
|
}
|
|
|
|
}
|
2019-01-16 21:59:02 +01:00
|
|
|
|
2019-01-16 11:32:15 +01:00
|
|
|
public function validate()
|
|
|
|
{
|
|
|
|
$result = parent::validate();
|
|
|
|
if ($this->MinValue > $this->MaxValue) {
|
2019-01-16 21:20:31 +01:00
|
|
|
$result->addError(
|
|
|
|
_t(__CLASS__ . '.ORDER_WARNING', 'Minimum length should be less than the maximum length.')
|
|
|
|
);
|
2019-01-16 11:32:15 +01:00
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
2014-02-10 00:40:20 +01:00
|
|
|
}
|