填充多选下拉菜单zend 3

问题描述

大家好,我很难在表单中填充多选下拉列表。 到目前为止,我一直在尝试为表单添加工厂,就像这样

class MovieFormFactory
{
    public function __invoke(ContainerInterface $container,$requestedname,array $options = null)
    {
        $entityManager = $container->get('doctrine.entitymanager.orm_default');
        $actors = $entityManager->getRepository(Actor::class)->findAll();
        $form = new MovieForm();
        $form->setActors($data);

        
        return $form;
    }

}

我的表格

Class MovieForm extends Form
{

    private $actors = [];
    public function setActors($actorsData){ 
      $this->actors = $actors
    }
    public function __construct()
    {
        parent::__construct('post-form');

        $this->setAttribute('method','post');

        $this->addElements();
        $this->addInputFilter();
        

      $this->add([
        'type'  => 'select','name' => 'actors','attributes' => [
            'id' => 'actors','multiple' => true
        ],'options' => [
            'label' => 'Actors','value_options' => $this->actors,],]);

        $this->add([
            'type'  => 'select','name' => 'directors','attributes' => [
                'id' => 'directors','options' => [
                'label' => 'Director','value_options' => $this->getoptionForSelect($directors),]);

    }

    /**
     * This method adds elements to form (input fields and submit button).
     */
    protected function addElements()
    {

        // Add "title" field
        $this->add([
            'type'  => 'text','name' => 'title','attributes' => [
                'id' => 'title'
            ],'options' => [
                'label' => 'Title',]);

        // Add "description" field
        $this->add([
            'type'  => 'textarea','name' => 'description','attributes' => [
                'id' => 'description'
            ],'options' => [
                'label' => 'Description',]);

        // Add "tags" field
//        $this->add([
//            'type'  => 'text',//            'name' => 'actors',//            'attributes' => [
//                'id' => 'actors',//                'multiple' => 'multiple'
//            ],//            'options' => [
//                'label' => 'Actors',//                'value_options' => $this->getoptionForSelect(),//            ],//        ]);

        // Add "status" field
        $this->add([
            'type'  => 'select','name' => 'status','attributes' => [
                'id' => 'status'
            ],'options' => [
                'label' => 'Status','value_options' => [
                    MovieStatusEnum::STATUS_DRAFT => MovieStatusEnum::STATUS_DRAFT,MovieStatusEnum::STATUS_PUBLISHED => MovieStatusEnum::STATUS_PUBLISHED,]
            ],]);

        // Add the submit button
        $this->add([
            'type'  => 'submit','name' => 'submit','attributes' => [
                'value' => 'Create','id' => 'submitbutton',]);
    }

    /**
     * This method creates input filter (used for form filtering/validation).
     */
    private function addInputFilter()
    {

        $inputFilter = new InputFilter();
        $this->setInputFilter($inputFilter);

        $inputFilter->add([
            'name'     => 'title','required' => true,'filters'  => [
                ['name' => 'StringTrim'],['name' => 'StripTags'],['name' => 'StripNewlines'],'validators' => [
                [
                    'name'    => 'StringLength','options' => [
                        'min' => 1,'max' => 1024
                    ],]);

        $inputFilter->add([
            'name'     => 'description','filters'  => [
                ['name' => 'StripTags'],'max' => 4096
                    ],]);

        $inputFilter->add([
            'name'     => 'actors',]);

        
    }

    private function getoptionForSelect($data)
    {
        foreach ($data as $person) {
            $selectData[$person->getId()] = $person->getName();
        }
        return $selectData;
    }

}

这是我在module.config.PHP注册的工厂

 'form_elements' => [
        'factories' => [
            Form\MovieForm::class => Form\Factory\MovieFormFactory::class
        ]
    ],

但是似乎没有任何作用,我在制作电影时无法显示我的演员,在编辑电影时无法显示选定的演员,请指导我,我是zend的新手。

解决方法

在构造函数中,行value_options' => $this->actors是错误的,因为尚未设置$this->actors。在您的工厂中写:

$form = new MovieForm();
$form->setActors($data);

因此,您必须在类setActors()中声明公共方法MovieForm,该方法将设置选项数组。

public function setActors($array)
{
    $this->get('actors')->setValueOptions($array);
}