0

我在我的 Laminas 项目中遇到了一个问题。在我的控制器中,我用参数调用了我的表单。我的表单包含字段集的添加功能,我想读取我的字段集中的参数。我怎样才能做到这一点 ?

这是我的控制器中的功能:

public function realisationImpactAction(){
        $post=$this->post;  
        $mapperProjet = $this->SM->get('TPROJET');
        $mapperDoc = $this->SM->get('TDOC');
        //$hydrator = new ArraySerializableHydrator();
        //$reflectionHydrator = new \Laminas\Hydrator\ReflectionHydrator();
        $supervision  = new Supervision();
        
        $id = (int) $this->params()->fromRoute('id');       
        if (0 === $id) {              
            return $this->redirect()->toRoute('front-supervision',['action' =>  'edition-projet','id' => $id]);
        }
        
        $projet = $mapperProjet->fetchRow(['idProjet' => $id]);
        $docs = $mapperDoc->getDocsByProjetId($id);
        
        $realisationImpactForm = $this->formManager->get(RealisationImpactForm::class,['idProjet' => $id,'idSupervision' => 1]);     
        
        //$realisationImpactForm->bind($supervision)->setInputFilter($supervision->getInputFilter())->setData($post);         
        if(!$post){
            return new ViewModel(['form' => $realisationImpactForm,'user' => $this->user,'id' => $id,'projet' => $projet,'docs' => $docs]);
        }                
              
    }

这是我的表格:

<?php

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

namespace Commun\Form\Modules\Application;

use Commun\Form\Modules\Application\Fieldset\RealisationImpactFieldset;


use Commun\Form\CommunForm;

class RealisationImpactForm extends CommunForm 
{
    
    /*private $params;
    public function __construct($params) {
        $this->params = $params;
        parent::__construct('RealisationImpactForm',$this->params);
        
        
    }*/
    public function init() {   
        //var_dump($this); // this shows correcty my params values 
        $this->setName('RealisationImpactForm');        
        $this->addFieldset(RealisationImpactFieldset::class,['use_as_base_fieldset' => true,'params' => $this->params]);
        $this->addSubmitButton('next', 'Poursuivre', 'next', 'btn btn-vert w-100');        
        $this->addSubmitButton('previous', 'Retour', 'previous', 'btn btn-rouge w-100');
    }  
  
}

这是我的字段集

<?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\Supervision;
use Laminas\InputFilter\InputFilterProviderInterface;
use Laminas\Hydrator\ReflectionHydrator;

use Commun\Form\CommunFormFieldset;


class RealisationImpactFieldset extends CommunFormFieldset implements InputFilterProviderInterface
{
        
    
    private $mapper;
    /*private $idProjet;
    private $idSupervision;*/
       
    public function __construct($mappers=[],$options=[])
    {       
        
        $this->mapper = $mappers;
        /*$this->idProjet = $idProjet;
        $this->idSupervision = $idSupervision;*/ 
        parent::__construct('RealisationImpactForm',$options);          
        $this->setHydrator(new ReflectionHydrator());
        $this->setObject(new Supervision());
        $this->setLabel('Realisation Impact');                  
    }
    
    
    public function init() {
        parent::init();   
        
        //$effets = $this->mapper['supervision']->getRapportEffetByProjetForSuivi($this->idProjet,$this->idSupervision);
        
        
        /*$this->addText('libelle','Intitulé de l\'activité','libelle',['class' => 'form-control champ-requis']);
        $options = $this->mapper['indicateur']->getOptions('idRefIndicateur','libelle','Aucune valeur choisie',null,['libelle']);           
        $this->addSelect('idRefIndicateur','Indicateur de performance',$options,['class' => 'form-control']);
        $this->addText('valeurReference','Valeur de référence','valeurReference',['class' => 'form-control champ_decimal']);
        $this->addText('valeurCible','Valeur cible','valeurCible',['class' => 'form-control champ_decimal']);
        $this->addTextarea('moyenVerification', 'Moyen de vérification', 'moyenVerification', ['class' => 'form-control','rows' => 3]); 
        $this->addTextarea('risqueMesureAttenuation', 'Risques et mesures d\'atténuation', 'risqueMesureAttenuation', ['class' => 'form-control','rows' => 3]);  */        
    }
    
        
    /**
     * @return array
     */
    public function getInputFilterSpecification()
    {
        return [
            
        ];
    }
}

问题是如何将我的参数放入字段集 __construct 或 init 函数中?

4

1 回答 1

0

有可能做到这一点,你离归档这个很近了。您只需要添加工厂来接收调用时传递的值Laminas\Form\FormElementManager::get()

这是一个例子:

<?php

use Laminas\Form\Fieldset;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Interop\Container\ContainerInterface;
use Laminas\Form\Form;
use Laminas\ServiceManager\AbstractPluginManager;

class MyFieldset extends Fieldset {

     private string $theValue = '';

     public function __construct(string $theValue, $name = null, array $options = []) {
         $this->theValue = $theValue;
         parent::__construct($name, $options);
     }

}

class MyFieldsetFactory implements FactoryInterface {

    /**
     * @return MyFieldset
     */
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null) {
        $theValue = $options['theValue'] ?? 'some_default';
        unset($options['theValue']);
        return new MyFieldset($theValue, null, $options);
    }

}

class MyForm extends Form {

    private string $theValue = '';

    // use the same logic from MyFieldset::__construct
    // and create a factory for this form, which uses the same logic as
    // MyFieldsetFactory::__invoke

    public function init(): void {
       // you can get the FormElementManager instance from the form factory,
       // which itself is always passed to an Form/Fieldset/Element by the FormElementManager
       // use the second parameter to pass options to the factory
       $myFieldset = $this->getFormFactory()->getFormElementManager()
            ->get(MyFieldset::class, ['theValue' => $this->theValue]);

       $this->add($myFieldset); // or add as collection
    }
}

class MyController {

    // pass the formElementManager instance by constructor
    private AbstractPluginManager $formElementManager;

    public function myAction()/*: ViewModel*/ {
        $this->formElementManager->get(MyForm::class, [
            'theValue' => rand(1, 100),
        ]);
    }

}

这里缺少一个重要部分:您需要将这些表单和字段集及其工厂添加到 FormElementManager 的配置中。

于 2021-09-23T09:54:18.323 回答