I am developing a Laminas-Form that requires dependencies from a database table.
#My form looks like this:
namespace Application\Form;
use Laminas\Form\Form;
class CountryForm extends Form
{
public function init()
{
parent::init();
$this->add([
'type' => CountryFieldset::class,
'name' => 'country_id',
'options' => [
'use_as_base_fieldset' => true
]
]);
}
}
#My Fieldset looks like this
namespace Application\Form;
use Application\Model\Table\CountriesTable;
use Laminas\Form\Element\Select;
use Laminas\Form\Fieldset;
class CountryFieldset extends Fieldset
{
public function __construct(CountriesTable $countriesTable)
{
$countries = $countriesTable->fetchAllCountries();
$selectField = new Select();
$selectField->setName('country_id')
->setOptions([
$selectField->setLabel('Countries'),
$selectField->setEmptyOption('Select...'),
$selectField->setValueOptions($countries);
])
->setAttributes([
'required' => true,
'class' => 'custom-select'
]);
$this->add($selectField);
}
}
#Factory looks like this:
namespace Application\Form;
use Application\Model\Table\CountriesTable;
use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\FactoryInterface;
use Laminas\ServiceManager\ServiceLocatorInterface;
class CountryFieldsetFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $name, array $options = null)
{
return new CountryFieldset($container->get(CountriesTable::class));
}
}
#My Controller Factory looks like this:
namespace Application\Controller;
use Interop\Container\ContainerInterface;
use Application\Form\CountryForm;
class IndexControllerFactory
{
public function __invoke(ContainerInterface $container)
{
$formManager = $container->get('FormElementManager');
return new IndexController($formManager->get(CountryForm::class));
}
}
#My controller looks like this:
namespace Application\Controller;
use Application\Form\MyForm;
use Laminas\Mvc\Controller\AbstractActionController;
class IndexController extends AbstractActionController
{
private $countryForm;
public function __construct(CountryForm $countryForm)
{
$this->countryForm = $countryForm;
}
public function indexAction()
{
return array('form' => $this->countryForm);
}
}
#I mapped the fieldset factory to look like this in the module.config.php file:
return [
'form_elements' => [
'factories' => [
Application\Form\CountryFieldset::class => Application\Form\CountryFieldsetFactory::class,
],
],
'controllers' => [
'factories' => [
Application\Controller\IndexController::class => Application\Controller\IndexControllerFactory::class,
],
],
];
#My form view looks like
<?php
echo $this->form()->openTag($form);
echo $this->formCollection($form->get('country_id'));
echo $this->form()->closeTag();
As you can see everything works fine. The Select option values display as I would like them to.
The issue I am having is that in the views the the select form attribute name comes up as
‘country_id[country_id]’
<fieldset>
<label>
<span>Countries</span>
<select name="country_id[country_id]" required="required" class="custom-select">
<option value="">Select...</option>
<option value="1">Afghanistan</option>
.
.
.
</select>
</label>
</fieldset>
as opposed to the expected attribute name of ‘country_id’:
<select name="country_id" required="required" class="custom-select">
Can anyone help me understand why that is the case?