Skip to content

Commit

Permalink
feat: add support for IsNumeric
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Jul 22, 2024
1 parent a6b7ac4 commit 3cc6fcc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/EditableKeyValueField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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';
Expand All @@ -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();
Expand All @@ -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);

Expand Down
41 changes: 40 additions & 1 deletion src/KeyValueField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
Expand All @@ -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();
Expand All @@ -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);
Expand Down

0 comments on commit 3cc6fcc

Please sign in to comment.