Hello everyone,
first of all I would like to thank you for your help in my Laminas training.
I faced a problem and I would like to submit it to you.
I have successfully created forms base of fieldset and everything work fine until I get a new problem.
I would like to create a form collection wich contains a select populate from my database (I have already done that). But when I add a fielset as base, I get error and I stuck on it. Here is my code :
My MontantPrestationFormFieldset :
namespace Admin\Form\Fieldset;
use Commun\Model\Entity\MontantPrestation;
use Laminas\InputFilter\InputFilterProviderInterface;
use Laminas\Hydrator\ReflectionHydrator;
use Commun\Form\CommunFormFieldset;
class MontantPrestationFormFieldset extends CommunFormFieldset implements InputFilterProviderInterface
{
private $refAnneeScolaire;
private $refClasse;
private $refTypeEcheancier;
private $refPrestation;
private $locale;
private $optionsEcheancier;
public function __construct($refAnneeScolaire,$refClasse,$refTypeEcheancier,$refPrestation,$optionsEcheancier,$locale)
{
parent::__construct('montantPrestationForm');
$this->refAnneeScolaire=$refAnneeScolaire;
$this->refClasse=$refClasse;
$this->refTypeEcheancier=$refTypeEcheancier;
$this->refPrestation=$refPrestation;
$this->optionsEcheancier=$optionsEcheancier;
$this->locale=$locale;
$this->setHydrator(new ReflectionHydrator());
$this->setObject(new MontantPrestation());
$this->setLabel('montantPrestation');
$optionsAnneeScolaire = $this->refAnneeScolaire->getOptions('idRefAnnee','libelle','aucune-valeur-choisie',null,['isDefault desc','annee']);
$optionsClasse = $this->refClasse->getOptions('idRefClasse','libelle','aucune-valeur-choisie',null,['ordre']);
$optionsTypeEcheancier = $this->refTypeEcheancier->getOptions('idRefTypeEcheancier','libelle_'.$this->locale,'aucune-valeur-choisie',null,['libelle_'.$this->locale]);
$optionsPrestation = $this->refPrestation->getOptions('idRefPrestation','libelle_'.$this->locale,'aucune-valeur-choisie',['isActif'=>1],['libelle_'.$this->locale]);
$this->addSelect('idRefPrestation', 'prestation-paiement',$optionsPrestation,['required' => 'required','class' => 'form-control show-tick']);
$this->addSelect('idRefAnnee', 'annee-scolaire-paiement',$optionsAnneeScolaire,['required' => 'required','class' => 'form-control show-tick']);
$this->addSelect('idRefClasse', 'classe-eleve-paiement',$optionsClasse,['required' => 'required','class' => 'form-control show-tick']);
$this->addText('montant', 'montant-paiement','',['required' => 'required','class' => 'form-control champ_montant']);
$this->addSelect('idRefTypeEcheancier', 'type-echeancier',$optionsTypeEcheancier,['required' => 'required','class' => 'form-control show-tick']);
$this->addCheckbox('isDefault', 'valeur-par-defaut', 'isDefault', '1', '0', ['class' => 'form-check-input','value' => '0']);
$this->addCollection('echeanciers', 'echeancier', new EcheancierClasseFieldset($this->optionsEcheancier, $this->locale));
//$this->addCollection('echeanciers', 'echeancier', 'Admin\Form\Fieldset\EcheancierClasseFieldset');
/*$this->add([
'type' => Collection::class,
'name' => 'classes',
'options' => [
'label' => 'Veuillez renseigner les classes de cet eleve',
'count' => 2,
'should_create_template' => true,
'allow_add' => true,
'target_element' => [
'type' => ClasseFieldset::class,
],
],
]);*/
}
/**
* @return array
*/
public function getInputFilterSpecification()
{
return [
];
}
}
My CommunFormFieldset
namespace Commun\Form;
use Laminas\Form\Element\Select;
use Laminas\Form\Fieldset;
use Laminas\Form\Element\Checkbox;
class CommunFormFieldset extends Fieldset //implements InputFilterProviderInterface
{
/**
* Shortcut for adding an input type text element
* @param string $name name of the item
* @param string $label label of the item
* @param string $id unique HTML id of the item
* @param array $attributes array of attributes to apply to the item
*/
public function __construct($name=null,$options = []) {
parent::__construct($name, $options);
}
public function addText($name,$label,$id='',$attributes=[]){
$this->add([
'name' => $name,
'type' => 'text',
'options' => [
'label' => $label,
'id' => $id,
],
'attributes' => $attributes,
]);
}
/**
* Shortcut for adding an select list type element
* @param string $name name of the select item
* @param string $label label of the select item
* @param array $valueOptions array values to populate the Select with
* @param array $attributes array of attributes to apply to the Select item
*/
public function addSelect($name,$label,$valueOptions=[],$attributes=[]){
$this->add([
'name' => $name,
'type' => Select::class,
'options' => [
'label' => $label,
'value_options' => $valueOptions,
],
'attributes' => $attributes,
]);
}
public function addCheckbox($name,$label,$id='',$checkedValue='',$unCheckedValue='',$attributes=[]){
$this->add([
'name' => $name,
'type' => Checkbox::class,
'options' => [
'label' => $label,
'id' => $id,
'use_hidden_element' => true,
'checked_value' => $checkedValue,
'unchecked_value' => $unCheckedValue,
],
'attributes' => $attributes,
]);
}
public function addCollection($name,$label, Fieldset $targetElement,$count=2,$shouldCreateTemplate=true,$allowAdd=true){
$this->add([
'type' => \Laminas\Form\Element\Collection::class,
'name' => $name,
'options' => [
'label' => $label,
'count' => $count,
'should_create_template' => $shouldCreateTemplate,
'allow_add' => $allowAdd,
'target_element' => [
'type' => $targetElement,
],
],
]);
}
}
The MontantPrestationFormFieldsetFactory
namespace Admin\Form\Fieldset\Factory;
use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;
class MontantPrestationFormFieldsetFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$translator = $container->get(\Laminas\I18n\Translator\TranslatorInterface::class);
$locale=strtolower(substr($translator->getLocale(), 0, 2));
$optionsEcheancier = $container->get('TREFECHEANCIER')->getOptions('idRefEcheancier','libelle_'.$locale,'aucune-valeur-choisie',null,['libelle_'.$locale]);
return new \Admin\Form\Fieldset\MontantPrestationFormFieldset($container->get('TREFANNEESCOLAIRE'),
$container->get('TREFCLASSE'),
$container->get('TREFTYPEECHEANCIER'),
$container->get('TREFPRESTATION'),
$optionsEcheancier,
$locale
);
}
}
My EcheancierClasseFieldset
namespace Admin\Form\Fieldset;
use Commun\Model\Entity\EcheancierClasse;
use Laminas\InputFilter\InputFilterProviderInterface;
use Laminas\Hydrator\ReflectionHydrator;
use Commun\Form\CommunFormFieldset;
class EcheancierClasseFieldset extends CommunFormFieldset implements InputFilterProviderInterface
{
private $optionsEcheancier=[];
private $locale;
public function __construct($optionsEcheancier, $locale)
{
parent::__construct('echeancierClasseForm');
$this->optionsEcheancier=$optionsEcheancier;
$this->locale=$locale;
$this->setHydrator(new ReflectionHydrator());
$this->setObject(new EcheancierClasse());
$this->setLabel('EcheancierClasse');
$optionsOrdre = [];
$optionsOrdre['']='aucune-valeur-choisie';
for($i=5;$i<=100;$i++){
if($i%5!=0){continue;}
$optionsOrdre[$i]=$i;
}
$optionsJourLimite = [];
$optionsJourLimite['']='aucune-valeur-choisie';
for($i=1;$i<=31;$i++){
$optionsJourLimite[$i]=$i;
}
//$optionsEcheancier = $this->refEcheancier->getOptions('idRefEcheancier','libelle_'.$this->locale,'aucune-valeur-choisie',null,['libelle_'.$this->locale]);
$this->addText('montant', 'montant-echeance', 'montant', ['required' => 'required','class' => 'form-control champ_montant']);
$this->addText('montantMinimum', 'montant-minimum-echeance', 'montant', ['required' => 'required','class' => 'form-control champ_montant']);
$this->addSelect('ordre', 'ordre-affichage',$optionsOrdre,['required' => 'required','class' => 'form-control show-tick']);
$this->addSelect('jourLimite', 'jour-limite',$optionsJourLimite,['required' => 'required','class' => 'form-control show-tick']);
$this->addSelect('idEcheancierPeriodeLimite', 'echeance-limite',$this->optionsEcheancier,['required' => 'required','class' => 'form-control show-tick']);
}
/**
* @return array
*/
public function getInputFilterSpecification()
{
return [
];
}
}
In my controller, I call the form like this
$montantPrestationForm = $this->formManager->get(\Admin\Form\MontantPrestationForm::class);
I finally get this error in my view
File:
C:\wamp\www\school-online\vendor\laminas\laminas-servicemanager\src\ServiceManager.php:262
Message:
Argument 1 passed to Laminas\ServiceManager\ServiceManager::Laminas\ServiceManager\{closure}() must be of the type string, object given, called in C:\wamp\www\school-online\vendor\laminas\laminas-servicemanager\src\Servic
I need to say that in my MontantPrestationFormFieldset, when I comment the line
$this->addCollection(‘echeanciers’, ‘echeancier’, new EcheancierClasseFieldset($this->optionsEcheancier, $this->locale))
everything works fine.
Any help or ideas will be very appreciated