Laminas fieldset unable to use custom select with where clause

I have a laminas form wich contains a fieldset. In this fieldset, I have a custom select Element wich is populate from database with a where clause depending on parameter passed to my fieldset. However, when I use my custom Select, I get this error message <<Argument 1 passed to Laminas\ServiceManager\ServiceManager::Laminas\ServiceManager{closure}() must be of the type string, object given, called in…>>.

I post my code below : My fieldset here :

<?php

/**
    * @module     Commun
    * @subpackage Form\Admin
    * @author     Samuel NANGUI <nanguisamuel@gmail.com>
    * @copyright  Copyright (c) 2020 Nslabs
    */

    namespace Commun\Form\Modules\Application\Fieldset;
    
    use Commun\Model\Entity\RapportEffet;
    use Laminas\InputFilter\InputFilterProviderInterface;
    use Laminas\Hydrator\ReflectionHydrator;
    
    use Laminas\Filter\StripTags;
    use Laminas\Filter\StringTrim;
    
    use Commun\Form\CommunFormFieldset;
    use Commun\Form\SelectOptions\EffetsProjetSelect;
    
    class EffetRapportFieldset extends CommunFormFieldset implements InputFilterProviderInterface
    {
            
        
        private $mapper;
        private $inputFilter;   
        private $params;
           
        public function __construct($mappers=[],$params=[], $options = [])
        {        
            $this->mapper = $mappers;
            $this->params = $params;        
            parent::__construct($this->mapper,'EffetRapportForm',$options);           
            $this->setHydrator(new ReflectionHydrator());
            $this->setObject(new RapportEffet());          
            
        }
        
            
        public function init() {       
            //parent::init();  
            //var_dump($this->params);
            //$idProjet = $this->params;        
            //$optionsEffetCadreLogique = $this->mapper['effetProjet']->getOptions('idEffetProjet','libelleEffet','Selectionner',['idProjet' => $idProjet],['libelleEffet']);                                         
            //$this->addSelect('idEffetProjet', 'Intitule de l\'effet',[],['class' => 'form-control'],'champ-requis'); 
            $mySelect = $this->getFormFactory()->getFormElementManager()->get(EffetsProjetSelect::class, ['params' => $this->params]);                
            //$this->addSelectFromTable('idEffetProjet','Intitule de l\'effet', $mySelect,['class' => 'form-control champ-affiche'],'champ-requis');
            $this->add([            
                'name' => 'idEffetProjet',              
                'type' => $mySelect,            
                'options' => [
                    'label' => 'Intitule de l\'effet',  
                    'label_attributes' => [
                        'class'  => 'form-control champ-affiche'
                    ],               
                ],            
                'attributes' => 'champ-requis',
            ]); 
            $this->addText('valeurReference','Valeur de référence (a)',NULL,['class' => 'form-control champ_decimal champ-affiche champ_valeur_reference'],'champ-requis');
            $this->addText('valeurRecente','Valeur la plus récente (b)',NULL,['class' => 'form-control champ_decimal champ_valeur_recente'],'champ-requis');
            $this->addText('cibleFinale','Cible finale (c)',NULL,['class' => 'form-control champ_decimal champ-calcule champ_valeur_cible'],'champ-requis');
            $this->addText('progresRealisation','Progrès vers la réalisation de la cible (% de réalisation) (d=b/c)',NULL,['class' => 'form-control champ_decimal champ-affiche champ_valeur_progres'],'champ-requis');
            $this->addTextarea('evaluation', 'Évaluation', NULL, ['class' => 'form-control','rows' => 3]);               
        }
        
            
        /**
         * @return array
         */
        public function getInputFilterSpecification()
        {
            return [
                
            ];       
        }
    }

My custom Select here :

<?php

/**
    * @module     Commun
    * @subpackage Form\Admin
    * @author     Samuel NANGUI <nanguisamuel@gmail.com>
    * @copyright  Copyright (c) 2021 Nslabs
    */

namespace Commun\Form\SelectOptions;


use Laminas\Form\Element\Select;

class EffetsProjetSelect extends Select 
{
     
    private $repository;
    private $params;
    public function __construct($repository,$params)
    {        
        //parent::__construct($name, $options);
        $this->repository = $repository;                
        $this->params = $params;
    }
        
    /**
     * Initialize the element
     *
     * @return void
     */
    public function init() {        
        $idProjet=$this->params;
        //var_dump($idProjet);
        $options = $this->repository->getOptions('idEffetProjet','libelleEffet','Sélectionner',['idProjet' => $idProjet],NULL);                                                 
        //$options = $this->repository->getOptions('idEffetProjet','libelleEffet','Sélectionner',NULL,['libelleEffet']);                                                 
        $this->setValueOptions($options);
    } 
  
}

The factory for my custom Select

<?php

/**
    * @module     Commun
    * @subpackage Form
    * @author     Samuel NANGUI <nanguisamuel@gmail.com>
    * @copyright  Copyright (c) 2020 Nslabs
    */

namespace Commun\Factory\Form\SelectOptions;


use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Commun\Form\SelectOptions\EffetsProjetSelect;

class EffetsProjetSelectFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {                
        $params = $options['params'] ?? [];
        unset($options['params']);    
        $mapper=$container->get('TEFFETPROJET');       
        return new EffetsProjetSelect($mapper,$params);
    }
}

I think the mapper property of EffetRapportFieldset is misleading here. Can you remove it?

Have you registered your custom element? See the configuration key form_elements:

https://docs.laminas.dev/laminas-form/v3/advanced/#creating-custom-elements

1 Like

Yes I have registered my form Element like this :

'form_elements' => [
        'factories' => [   
            Form\Modules\Application\Fieldset\ProjetFieldset::class => Factory\Form\Fieldset\ProjetFieldsetFactory::class,
            Form\Modules\Application\Fieldset\EffetFieldset::class => Factory\Form\Fieldset\EffetFieldsetFactory::class,
            Form\Modules\Application\Fieldset\ComposanteFieldset::class => Factory\Form\Fieldset\ComposanteFieldsetFactory::class,
            Form\Modules\Application\Fieldset\ProduitFieldset::class => Factory\Form\Fieldset\ProduitFieldsetFactory::class,
            Form\Modules\Application\Fieldset\ActiviteFieldset::class => Factory\Form\Fieldset\ActiviteFieldsetFactory::class,
            Form\Modules\Application\Fieldset\DocFieldset::class      => Factory\Form\Fieldset\DocFieldsetFactory::class,
            Form\Modules\Application\Fieldset\RealisationImpactFieldset::class => Factory\Form\Fieldset\RealisationImpactFieldsetFactory::class,  
            Form\Modules\Application\Fieldset\EffetRapportFieldset::class => Factory\Form\Fieldset\EffetRapportFieldsetFactory::class,
            Form\Modules\Application\Fieldset\RapportEffetFieldset::class => Factory\Form\Fieldset\RapportEffetFieldsetFactory::class,
            Form\Modules\Application\RapportEffetForm::class => Factory\Form\RapportEffetFormFactory::class,
            
            //Select Options
            Form\SelectOptions\RefNotationSelect::class => Factory\Form\SelectOptions\RefNotationSelectFactory::class,
            Form\SelectOptions\EffetsProjetSelect::class => Factory\Form\SelectOptions\EffetsProjetSelectFactory::class,            
        ],        
    ],

I started removing $mapper from my form and fieldset as you recommended. I do not use this mapper in my fieldset and I will remove it.

I mean here in your example!

Can you provide the entire error message with stack trace?

I need to say that RefNotationSelect is working well because there is not param on it.
But the second one stucks … :unamused:


Here it is

The important part is missing: Stack #0 – which object? EffetsProjetSelect or RefNotationSelectFactory?

And which version of laminas-servicemanager do you use? The line numbers do not match with version 3.10.0.

here is the complete line

First message :
Argument 1 passed to Laminas\ServiceManager\ServiceManager::Laminas\ServiceManager\{closure}() must be of the type string, object given, called in C:\wamp\www\sigpme\vendor\laminas\laminas-servicemanager\src\ServiceManager.php on line 270

Stack :
#0 C:\wamp\www\sigpme\vendor\laminas\laminas-servicemanager\src\ServiceManager.php(270): Laminas\ServiceManager\ServiceManager::Laminas\ServiceManager\{closure}(Object(Commun\Form\SelectOptions\EffetsProjetSelect))

And where can I get the version of laminas-servicemanager ?

Found the problem. This does not work:

$this->add(['type' => new MyElement()]);

The method add calls the form element manager again via the factory, if an array is given.

Use:

$this->add(new MyElement());

Ok let me try and I will get back to you in 10min max