How to use UUIDs or strings with dots as form field keys

Scarf
Member | 2
+
0
-

I am currently playing around with the forms component in a standalone scenario. Suppose I have a list of items identified by UUIDs. How can these IDs be registered as form keys?

Example

$form = new Form;
$items = $form->addContainer('item');

// Example: item[444c5d44-383c-4497-8fcf-26d502bcb446] = 'a.a'
$items->addRadioList(
    '444c5d44-383c-4497-8fcf-26d502bcb446',
    'Item 444c5d44-383c-4497-8fcf-26d502bcb446',
    ['a.a' => 'Option A', 'b.b' => 'Option B']
);

echo $form->render();

Resulting Error

php_1      | NOTICE: PHP message: Nette\InvalidArgumentException: Component name must be non-empty alphanumeric string, '444c5d44-383c-4497-8fcf-26d502bcb446' given. in /app/vendor/nette/component-model/src/ComponentModel/Container.php:49
php_1      | Stack trace:
php_1      | #0 /app/vendor/nette/forms/src/Forms/Container.php(276): Nette\ComponentModel\Container->addComponent(Object(Nette\Forms\Controls\RadioList), '444c5d44-383c-4...', NULL)
php_1      | #1 /app/vendor/nette/component-model/src/ComponentModel/ArrayAccess.php(28): Nette\Forms\Container->addComponent(Object(Nette\Forms\Controls\RadioList), '444c5d44-383c-4...')
php_1      | #2 /app/vendor/nette/forms/src/Forms/Container.php(412): Nette\Forms\Container->offsetSet('444c5d44-383c-4...', Object(Nette\Forms\Controls\RadioList))
php_1      | #3 /app/index.php(52): Nette\Forms\Container->addRadioList('444c5d44-383c-4...', 'Item 444c5d44-3...', Array)
php_1      | #4 {main}

After looking at the trace and the source code, I'm afraid that this behavior works as intended and there is no support for UUIDs or alpha-numeric strings that also allow dot and minus characters. Am I right in this concern?

Marek Bartoš
Nette Blogger | 569
+
+3
-

Convert UID to different format, e.g. base58. Symfony UID has methods for that https://symfony.com/…nts/uid.html#…

emololftw
Member | 33
+
+1
-

You can use php’s native functions hex2bin / bin2hex

Scarf
Member | 2
+
+1
-

Thank you for sharing the encoding/decoding-based solutions. Personally, I would like to avoid changing the form interface that already exists in a legacy system.