From 3cc6fcc9adc3f4c9122b5fe4c07484f663625dcb Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Mon, 22 Jul 2024 16:16:22 +1200 Subject: [PATCH] feat: add support for IsNumeric --- src/EditableKeyValueField.php | 18 ++++++++++++--- src/KeyValueField.php | 41 ++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/EditableKeyValueField.php b/src/EditableKeyValueField.php index 99fe2b3..3612c29 100644 --- a/src/EditableKeyValueField.php +++ b/src/EditableKeyValueField.php @@ -2,7 +2,9 @@ namespace FullscreenInteractive\KeyValueField; +use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\FieldList; +use SilverStripe\Forms\NumericField; use SilverStripe\Forms\TextareaField; use SilverStripe\UserForms\Model\EditableFormField; @@ -17,7 +19,8 @@ class EditableKeyValueField extends EditableFormField private static $plural_name = 'Key Value Fields'; private static $db = [ - 'Keys' => 'Text' + 'Keys' => 'Text', + 'IsNumeric' => 'Boolean', ]; private static $table_name = 'EditableKeyValueField'; @@ -29,6 +32,7 @@ public function getCMSFields() { $this->beforeUpdateCMSFields(function (FieldList $fields) { $fields->addFieldsToTab('Root.Main', TextareaField::create('Keys')->setDescription('One key per line')); + $fields->addFieldsToTab('Root.Main', CheckboxField::create('IsNumeric', 'Validate field values as numeric values?')->setDescription('Validates that all the values are numeric')); }); return parent::getCMSFields(); @@ -37,8 +41,16 @@ public function getCMSFields() public function getFormField() { - $field = KeyValueField::create($this->Name, $this->Title ?: false) - ->setKeys($this->getKeysAsArray()); + $field = KeyValueField::create($this->Name, $this->Title ?: false); + + if ($this->IsNumeric) { + $field->setValueFieldClass(NumericField::class); + $field->setFieldCallback(function ($field) { + $field->setHTML5(true); + }); + } + + $field->setKeys($this->getKeysAsArray()); $this->doUpdateFormField($field); diff --git a/src/KeyValueField.php b/src/KeyValueField.php index 5c59fe6..00d0b96 100644 --- a/src/KeyValueField.php +++ b/src/KeyValueField.php @@ -3,6 +3,7 @@ namespace FullscreenInteractive\KeyValueField; use SilverStripe\Core\Convert; +use SilverStripe\Core\Injector\Injector; use SilverStripe\Forms\CompositeField; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\LabelField; @@ -16,6 +17,16 @@ class KeyValueField extends CompositeField */ protected $keys = 0; + /** + * @var string + */ + protected $fieldClass = TextField::class; + + /** + * @var ?callable + */ + protected $fieldCallback = null; + public function __construct($name, $title = null, $value = null) { @@ -42,6 +53,28 @@ public function HolderID() } + /** + * Set the class to use for the value fields. + */ + public function setValueFieldClass(string $class) + { + $this->fieldClass = $class; + + return $this; + } + + + /** + * Set a callback to be called on each field. + */ + public function setFieldCallback(callable $callback) + { + $this->fieldCallback = $callback; + + return $this; + } + + public function buildChildren() { $children = new FieldList(); @@ -54,8 +87,14 @@ public function buildChildren() $fieldName = sprintf("%s[%s]", $name, $i); $value = isset($this->value[$i]) ? $this->value[$i] : ''; - $field = TextField::create($fieldName, $key, $value) + $field = Injector::inst()->create($this->fieldClass, $fieldName, $key, $value) ->addExtraClass('key__value'); + + if (is_callable($this->fieldCallback)) { + // call fieldCallback + ($this->fieldCallback)($field); + } + $this->invokeWithExtensions('updateKeyValueField', $field, $key, $i); $children->push($field);