0

重複の可能性:
ZendFramework2でドロップダウンリストを作成します

zf2を使い始めました。

データベースからオプションを取得するフォーム内にドロップダウンリストを作成する方法を考えていましたか?

これは初心者の質問かもしれませんが、私はそれをするのに苦労しています。

ありがとう

4

1 に答える 1

1

あなたはこれを行うことができます:

Fieldset を拡張するクラスを作成します。

このフィールドに何らかの検証が必要な場合は、クラス InputFilterProviderInterface を実装します。

サービス マネージャーにアクセスするには、ServiceLocatorAwareInterface と setServiceLocator () および getServiceLocator () の 2 つのメソッドを実装する必要があります。

デフォルトでは、Zend Framework MVC は、Zend\ServiceManager\ServiceLocatorAwareInterface を実装する任意のクラスに ServiceManager インスタンスを挿入するイニシャライザを登録します。これを読む

namespace Users\Form;

use Zend\InputFilter\InputFilterProviderInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Form\Element;
use Zend\Form\Fieldset;

class GroupsFieldset extends Fieldset implements InputFilterProviderInterface, 
        ServiceLocatorAwareInterface
{

    /**
     * @var ServiceLocatorInterface
     */
    protected $serviceLocator;

    public function __construct ()
    {
        parent::__construct('groups');

        $this->setLabel('Group');

        $this->setName('groups');

        $sl = $this->getServiceLocator();

        $sm = $sl->get('Users\Model\GroupsTable');

        $groups = $sm->fetchAll();

        $select = new Element\Select('groups');

        $options = array();

        foreach ($groups as $group) {
            $options[$group->id] = $group->name;
        }

        $select->setValueOptions($options);
    }

    /**
     * Set serviceManager instance
     *
     * @param  ServiceLocatorInterface $serviceLocator
     * @return void
     */
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
        return $this;
    }

    /**
     * Retrieve serviceManager instance
     *
     * @return ServiceLocatorInterface
     */
    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }

    /**
     * 
     * @return multitype:multitype:boolean
     */
    public function getInputFilterSpecification ()
    {
        return array(
                'name' => array(
                        'required' => true
                )
        );
    }
}

module.php よりも

public function getServiceConfig()
{
    return array(
            'factories' => array(
                    'Users\Model\UsersTable' =>  function($sm) {
                        $dbAdapter1 = $sm->get('Zend\Db\Adapter\Adapter');
                        $table1     = new UsersTable($dbAdapter1);
                        return $table1;
                    }, // Adapter and table groups here
                    'Users\Model\GroupsTable' =>  function($sm) {
                        $dbAdapter2 = $sm->get('Zend\Db\Adapter\Adapter');
                        $table2     = new GroupsTable($dbAdapter2);
                        return $table2;
                    },
            ),
    );
}

そして最後にあなたのフォームで

    $groups = new GroupsFieldset();
    $this->add($groups);
于 2012-09-19T12:44:48.320 に答える