1
votes

I've a problem with dropdown list with Zend Framework 2 & Doctrine. I would put the "selected" attribute on my dropdown list but all options pass to selected

My code :

Controller :

public function editAction()
{
    // get error message during addAction
    $this->layout()->setVariable("messageError", $this->flashMessenger()->getErrorMessages());

    $auth = $this->getAuthService();
    if ($auth->hasIdentity()){
        $builder = new AnnotationBuilder();
        // Get id of StaticContent
        $id = (int)$this->getEvent()->getRouteMatch()->getParam('id');
        if (!$id) {
            $this->flashMessenger()->addErrorMessage("Aucun plan choisi !");
             return $this->redirect()->toRoute('admin/plans');
        }
        $plan = $this->getEntityManager()->getRepository("Admin\Entity\Plan")->find((int)$id);
        $form = $builder->createForm($plan);
        // Find options for Localite list (<select>)
        $localites = $this->getEntityManager()->getRepository("Admin\Entity\Localite")->getArrayOfAll();
        $form->get('localiteid')->setValueOptions($localites);
        $form->get('localiteid')->setValue("{$plan->getLocaliteid()->getId()}");

        // Find options for TypePlan list (<select>)
        $typesPlan = $this->getEntityManager()->getRepository("Admin\Entity\TypePlan")->getArrayOfAll();
        $form->get('typeid')->setValueOptions($typesPlan);
        $form->get('typeid')->setValue("{$plan->getTypeid()->getId()}");
        // Options for Statut list (<select>)
        $form->get('statut')->setValueOptions(array('projet'=>'Projet', 'valide'=>'Validé'));
        $form->get('statut')->setValue($plan->getStatut());
        $form->setBindOnValidate(false);
        $form->bind($plan);
        $form->add(array(
            'name' => 'submit',
            'attributes' => array(
                'type'  => 'submit',
                'value' => 'Modifier',
                'id' => 'submitbutton',
                'class' => "btn btn-primary"
            ),  
        ));
        $request = $this->getRequest();
        if ($request->isPost()) {
            [...]

            }
}

With

$localites = $this->getEntityManager()->getRepository("Admin\Entity\Localite")->getArrayOfAll();
    $form->get('localiteid')->setValueOptions($localites);

i populate my dropdown correctly, normally with

$form->get('localiteid')->setValue("{$plan->getLocaliteid()->getId()}");

just set "selected" on option defined by :

$plan->getLocaliteid()->getId()

So why all options are selected in my dropdown ?!

Information : It's the same for typeId but no Statut

3

3 Answers

1
votes

It's probably not working because of the curly braces. According to the PHP documentation

Using single curly braces ({}) will not work for accessing the return values of functions or methods or the values of class constants or static class variables.

This is also unnecessary when using setValue. ZF2 will convert it to a string when formatting it in the view.

When you create the arrays to pass to setValueOptions() you should make it an associative array of arrays with the following values:

$form->get('select')->setValueOptions(array(
  'field' => array(
      'value' => 'value_of_the_option',
      'label' => 'what is displayed',
      'selected' => true,
  ),
));

Which ever of the fields has the selected option set to true will be the default selection in the form element.

1
votes

Personally i don't know if getArrayOfAll() such function exists, i assume that you are correctly passing array to FORM,

I think you should be doing something like this to set value.

 $form->get('localiteid')->setValue($plan->getLocaliteid()->getId());

But Since you are populating DROP down i guess this approach will not work best with Drop Down. You need to do something like this

   $form->get('localiteid')->setAttributes(array('value'=>$plan->getLocaliteid()->getId(),'selected'=>true));
0
votes

I've found a bug ?!

$plan = $this->getEntityManager()->getRepository("Admin\Entity\Plan")->find((int)$id);
$idLocalite = 18;//(int)$plan->getLocaliteid()->getId();
$idTypePlan = 2;//(int)$plan->getTypeid()->getId();

When i'm using $plan->getLocaliteid()->getId(); or $plan->getTypeid()->getId() to pass parameter into Repository method getArrayOfAll($idLocalite)

LocaliteRepository.php :

class LocaliteRepository extends EntityRepository {

  public function getArrayOfAll($currentLocaliteId) {
    $result = $this->_em->createQuery("SELECT l.nom, l.localiteid FROM Admin\Entity\Localite l ORDER BY l.nom")->getArrayResult();
    $localite = array();
    foreach($result as $loc) {
        if ($currentLocaliteId == $loc['localiteid']) {
            $localite[$loc['localiteid']] = array(
                    'value' => $loc['localiteid'],
                    'label' => $loc['nom'],
                    'selected' => true,
            );
        } else {
            $localite[$loc['localiteid']] = array(
                    'value' => $loc['localiteid'],
                    'label' => $loc['nom'],
                    'selected' => false
            );
            //$localite[$loc['localiteid']] = $loc['nom'];
        }
    }

    return $localite;
  }

}

So, if i'm using $idLocalite = 18 instead of $idLocalite = (int)$plan->getLocaliteid()->getId() only wanted option are selected. Why ?!