Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8539681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:25:27+00:00 2026-06-11T11:25:27+00:00

I know this sounds much more basic, still I want to post my question

  • 0

I know this sounds much more basic, still I want to post my question as it is related to Zend Framework 2. I know this form from the Zend example module

namespace Album\Form;

use Zend\Form\Form;

class AlbumForm extends Form
{
    public function __construct($name = null)
    {
        // we want to ignore the name passed
        parent::__construct('album');
        $this->setAttribute('method', 'post');
        $this->add(array(
            'name' => 'id',
            'attributes' => array(
                'type'  => 'hidden',
            ),
        ));
        $this->add(array(
            'name' => 'artist',
            'attributes' => array(
                'type'  => 'text',
            ),
            'options' => array(
                'label' => 'Artist',
            ),
        ));
        $this->add(array(
            'name' => 'title',
            'attributes' => array(
                'type'  => 'text',
            ),
            'options' => array(
                'label' => 'Title',
            ),
        ));
        $this->add(array(
            'name' => 'submit',
            'attributes' => array(
                'type'  => 'submit',
                'value' => 'Go',
                'id' => 'submitbutton',
            ),
        ));
    }
}

And this is called in this fashion

<?php
$form = $this->form;
$form->setAttribute('action', $this->url('album', array('action' => 'add')));
$form->prepare();

echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('title'));
echo $this->formRow($form->get('artist'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();

How can I add a drop down list for the artist field where the list is stored in an associative array. Since Im getting into Zend Framework 2, I wanted the suggestions from the experts. I have followed this previous post but it was somewhat unclear to me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:25:28+00:00Added an answer on June 11, 2026 at 11:25 am

    This is one way to do it for static options.

    ....
    
    $this->add(array(
        'type' => 'Zend\Form\Element\Select',
        'name' => 'number'
        'options' array(
            'options' => array( '1' => 'one', '2', 'two' )
        )
    ));
    

    Be warned….

    Because you are creating the form within a constructor you will not have access the ServiceManger. This could cause a problem if you want to populate from a database.

    Lets try something like…

    class AlbumForm extends Form implements ServiceManagerAwareInterface
    {
    
    public function __construct()
    {
        ....
    
        $this->add(array(
            'type' => 'Zend\Form\Element\Select',
            'name' => 'number'
        ));
    
        ....
    }
    

    ….

    public function initFormOptions()
    {
        $this->get('number')->setAttribute('options', $this->getNumberOptions());
    }
    
    protected function getNumberOptions()
    {
        // or however you want to load the data in
        $mapper = $this->getServiceManager()->get('NumberMapper');
        return $mapper->getList();
    }
    
    public function getServiceManager()
    {
        if ( is_null($this->serviceManager) ) {
            throw new Exception('The ServiceManager has not been set.');
        }
    
        return $this->serviceManager;
    }
    
    public function setServiceManager(ServiceManager $serviceManager)
    {
        $this->serviceManager = $serviceManager;
    }
    

    But that’s not great, rethink…

    Extending the Form so that you can create a form isn’t quite right. We are not creating a new type of form, we are just setting up a form. This calls for a factory. Also, the advantages of using a factory here are that we can set it up in a way in which we can use the service manager to serve it up, that way the service manager can inject itself instead of us doing in manually from the controller. Another advantage is that we can invoke this form whenever we have the service manager.

    Another point worth making is that where it makes sense, I think it’s better to take code out of the controller. The controller is not a script dump so it’s nice to have objects look after themselves. What I’m trying to say is that it’s good to inject an object with objects it needs, but it’s not okay to just hand it the data from the controller because it creates too much of a dependency. Don’t spoon feed objects from the controller, inject the spoon.

    Anyway, too much rant more code…

    class MySpankingFormService implements FactoryInterface
    {
        public function createService(ServiceLocatorInterface $serviceManager )
        {
            $mySpankingNewForm = new Form;
    
            // build that form baby,
            // you have a service manager,
            // inject it if you need to,
            // otherwise just use it.
    
            return $mySpankingNewForm;
        }
    }
    

    controller

    <?php
    
    class FooController
    {
        ...
        protected function getForm()
        {
            if ( is_null($this->form) ) {
                $this->form =
                    $this->getServiceManager()->get('MySpankingFormService');
            }
            return $this->form;
        }
        ...
    }
    

    module.config.php

    ...
    'service_manager' => array (
            'factories' => array (
                ...
                'MySpankingFormService'
                    => 'MyNameSpacing\Foo\MySpankingFormService',
                ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know much about web development, so probably this question will sound exceptionally
I know this sounds confusing: I have just built some basic prev/next pagination for
I know this sounds like a dumb question but I need to ask this.
I know the question sounds silly, but consider this: I have an array of
I know this sounds like a dumb question, but it's to settle an argument
I know this may sounds like a pointless question, but hear me out... I
i know this sounds really common and so trivial but , am having a
I know this sounds like a homework assignment, but it isn't. Lately I've been
I know this sounds somewhat counterintuitive, but let me explain what I am trying
Do browser vendors optimize against jQuery? I know this sounds absurd anti-standard, but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.