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

97 lines
2.4 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\UserForms\Model\EditableFormField;
use SilverStripe\Forms\FieldGroup;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\NumericField;
use SilverStripe\UserForms\Model\EditableFormField;
/**
* 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
private static $has_placeholder = true;
private static $db = [
2016-07-21 07:53:59 +02:00
'MinValue' => 'Int',
'MaxValue' => 'Int'
];
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->Title ?: false, $this->Default)
->setFieldHolderTemplate(EditableFormField::class . '_holder')
->setTemplate(EditableFormField::class)
2016-07-21 07:53:59 +02:00
->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(__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;
}
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);
}
}
public function validate()
{
$result = parent::validate();
if ($this->MinValue > $this->MaxValue) {
$result->addError(
_t(__CLASS__ . '.ORDER_WARNING', 'Minimum length should be less than the maximum length.')
);
}
return $result;
}
}