Concrete 8.5.7 $form->select() id and name

I need a form->select that has a different id and name.
On the /src/Form/Service/Form.php page I see $str = '<select id="' . $id . '" name="' . $key . '"' . $this->parseMiscFields('form-control', $miscFields) . '>';
So, I should be able to make the id and name different?

If you want non-standard behavior, why not use plain html/php?

I’m not sure how to get plain html to populate the variable options.

Are you suggesting there is no way to do it? If not, I have a work around, but it is causing me to edit the core code just slightly. I’d rather not edit it.
From ‘$str = ‘<select id="’ . $id . ‘" name="’ . $key . ‘"’ . $this->parseMiscFields(‘form-control’, $miscFields) . ‘>’;’
To ‘$str = ‘<select’ . (empty($key) ? ‘’ : ’ id="’ . $id . ‘" name="’ . $key . ‘"’) . $this->parseMiscFields(‘form-control’, $miscFields) . ‘>’;’
Works pretty good.

If you don’t want to override core file, you can write select field manually, like:

$app = Concrete\Core\Support\Facade\Application::getFacadeApplication();
$request = $app->make(Concrete\Core\Http\Request::class);

$options = [
    '' => '---',
    'First value' => 'First value',
    'Second value' => 'Second value',
<select name="your_name" id="your_id">
    <?php foreach ($options as $k => $v): ?>
        <option value="<?php echo h($k); ?>"
                <?php if ((string) $k === (string) $request->get('your_name')): ?>selected="selected"<?php endif; ?>
        ><?php echo h($v); ?></option>
    <?php endforeach; ?>

I think you can set a custom ID field in the additional parameters and that will override the default ID (which is generated from the name)

One of the advantages of the core form helper is that it perpetuates input values. That can come in useful if a form fails to validate. It saves the user from having to input everything again.

Having said that, I always try and code usage of forms to not depend on a field having an id="" attribute. There are so many ways relying on an id can go wrong.

This way should work, I’ll just have to figure out how to pull the data from the existing “$options”. I’ll see what I can figure out. Thank you.

This is the code I used:
<?= $form->select('', $manufacturers, $product->getManufacturer() ? $product->getManufacturer()->getID() : '', ['id' => 'pManufacturer' . $pID, 'name' => 'pManufacturer[]', 'class' => 'selectize pricing-fields']) ?>

It did this:
<select id="" name="" id="pManufacturer1589" name="pManufacturer[]" class="selectize pricing-fields form-control">

You can override “id” attribute in c5.9, but not in c5.8 sadly. I find ability to sometimes override id quite handy when working with javascript.

So, I have 1 of 3 options.

  1. Upgrade
  2. Manually
  3. Edit core file

Thank you for letting me know.

You can also override core file “concrete/src/Form/Service/Form.php” by putting modified file into “application/src/Form/Service/Form.php” (need to add some code into bootstrap/app.php) if you really want.

For your own use of forms, it may be easier to do an extend of the core class rather than override it.

Or… @mdius you could explain why you need to have the ID be different from the name so, understanding what you’re trying to achieve, we might be able to suggest an alternative path that would make everything easier… :grin:

I have the <select id="" name="" id="pManufacturer1589" name="pManufacturer[]" class="selectize pricing-fields form-control"> in a loop, so the ID needs to be unique and the name needs to be an array.

I think I’m going to try to use JohntheFish’s advice and try to extend the class.
It is working with my overwrite, but I was hoping for a better method.