Your Location is: Home > Php

Laminas (Zend Framework) Form Collection and fileprg plugin

From: Ivory View: 1121 MoOgur 

Question

Form with fieldsets:

class OptionForm extends Form
{
    public function __construct()
    {
        parent::__construct('option-form');
    
        $this->setHydrator(new ClassMethodsHydrator(false));
        $this->setInputFilter(new InputFilter());        
        $this->setAttribute('method', 'post');        
        $this->setAttribute('enctype', 'multipart/form-data');
    
        $this->add([
            'type' => Fieldset\OptionFieldset::class,
            'options' => [
                'use_as_base_fieldset' => true
            ]
        ]);

       //Some code
    }
}

class OptionFieldset extends Fieldset implements InputFilterProviderInterface 
{
    public function __construct() 
    {
        parent::__construct('option');

        $this->setHydrator(new ClassMethodsHydrator(false));
        $this->setObject(new Option());
    
        $this->add([
            'type' => Element\Collection::class,
            'name' => 'optionValues',
            'options' => [
                'count' => 1,
                'should_create_template' => false,
                'allow_add' => true,
                'allow_remove' => true,
                'target_element' => [
                    'type' => OptionValueFieldset::class,
                ]
            ],
       ]);

    //Some code
    }

    public function getInputFilterSpecification()
    {
        return [
            //Some code
        ];
   }
}

class OptionValueFieldset extends Fieldset implements InputFilterProviderInterface
{
    public function __construct() 
    {
        parent::__construct('optionValue');

        $this->setHydrator(new ClassMethodsHydrator(false));
        $this->setObject(new OptionValue());

    
        //Add input for "remember me" field.
        $this->add([
            'type'  => 'file',
            'name' => 'image'
        ]);
    
        $this->add([
            'type'  => 'text',
            'name' => 'sortOrder'
       ]);

        //Some code
   }

    public function getInputFilterSpecification()
    {
         return [
              'image' => [
              'required' => false,
              'validators' => [
                  ['name'    => 'FileUploadFile'],
                  ['name'    => 'FileIsImage'],
                  [
                      'name'    => 'FileMimeType',
                      'options' => [
                          'mimeType'  => ['image/jpeg', 'image/png']
                      ]
                  ]
              ],
              'filters'  => [
                  [
                      'name' => 'FileRenameUpload',
                      'options' => [
                          'target' => __DIR__ . '/../../../../../public/img/trash/img.jpg',
                          'useUploadName' => false,
                          'useUploadExtension' => true,
                          'overwrite' => true,
                          'randomize' => true
                      ]
                  ]
              ]
         ],
              //Some code
         ];

Controller:

public function addAction()
{
    $form = new OptionForm();
    
    $prg = $this->fileprg($form);
    
    if ($prg instanceof \Laminas\Http\PhpEnvironment\Response) {
        return $prg;
    } 
    
    if (is_array($prg)) {
        if($form->isValid()) { 
             $data  = $form->getData();
        } 
    }
    
    return new ViewModel([
        'form' => $form
    ]);
}

The image file is successfully transferred to the specified location, but fileprg returns array data to Controller, without image. If I add the form field (input type="file") to OptionForm, everything works fine, but if to the collection, then no. Something I’m doing wrong, or it’s not possible?

$prg array contains this:

array:2 [
    "option" => array:4 [
        "name" => "Option name"
        "type" => "select"
        "sortOrder" => "1"
        "optionValues" => array:1 [
             0 => array:2 [
                "name" => "Option value name"
                "sortOrder" => "1"
             ]
         ]
     ]
    "csrf" => "abb042930b1736360cafd20e8413d5e4-5cfa6ef26e7c5bd0c2af97202191d943"
]

Or if Doctrine entity (One-to-Many):

Site\Entity\Option {
    #id: null
    #name: "Option name"
    #type: "select"
    #sortOrder: 1
    -optionValues: array:1 [
        0 => Site\Entity\OptionValue {
            #id: null
            #name: "Option value name"
            #image: null
            #sortOrder: 1
            -option: null
        }
     ]
}

Best answer