codefog / contao-haste

Haste is a collection of tools and classes to ease working with Contao
http://codefog.pl/extension/haste.html
MIT License
43 stars 24 forks source link

Reset a form field's ID to the field name #153

Closed fritzmg closed 3 years ago

fritzmg commented 3 years ago

\Contao\Widget::getAttributesFromDca will set the ID and the name of the form element to the same value. However, this makes it impossible to do something like this:

$form = new Form('foobar', 'GET', function(Form $form) use ($request) {
    return null !== $request && $request->query->has('submit');
});

$form->addFormField('foo1', [
    'inputType' => 'select', 
    'name' => 'foo[]',
    'options' => [
        'option1',
        'option2',
    ],
    'value' => $request->query->get('foo')[0],
]);

$form->addFormField('foo2', [
    'inputType' => 'select', 
    'name' => 'foo[]',
    'options' => [
        'option3',
        'option4',
    ],
    'value' => $request->query->get('foo')[1],
]);

i.e. grouped form elements with []. Without the change invalid HTML would be generated, as both selects would have the ID ctrl_foo[].

With the proposed change the ID will be ctrl_foo1 and ctrl_foo2 while the name attribute will stay at foo[].