0

I have a Fieldset with class members for the inputFilterSpecification:

class FileFieldset extends Fieldset implements InputFilterProviderInterface
{

    protected $mimeTypes = ['image/jpg', 'image/png', 'application/pdf'];
    protected $fileSizeMin = 0;
    protected $fileSizeMax = 1;

    public function setMimeTypes(array $mimeTypes): void
    {
        $this->mimeTypes = $mimeTypes;
    }

    public function setFileSizeMin($fileSizeMin): void
    {
        $this->fileSizeMin = $fileSizeMin;
    }
    public function setFileSizeMax($fileSizeMax): void
    {
        $this->fileSizeMax = $fileSizeMax;
    }

    public function init()
    {
        $this->add([
            'name' => 'file_file',
            'type' => File::class,
            'options' => [
                'label' => 'Datei',
            ],
        ]);
    }

    public function getInputFilterSpecification()
    {
        $filter = [];

        $filter['file_file'] = [
            'required' => true,
            'filters' => [
            ],
            'validators' => [
                [
                    'name' => MimeType::class,
                    'break_chain_on_failure' => true,
                    'options' => [
                        'enableHeaderCheck' => true,
                        'disableMagicFile' => true,
                        'magicFile' => null,
                        'mimeType' => $this->mimeTypes,
                    ]
                ],
                [
                    'name' => Size::class,
                    'options' => [
                        'min'           => $this->fileSizeMin,
                        'max'           => $this->fileSizeMax,
                        'useByteString' => true,
                    ],
                ],
            ],
        ];

        return $filter;
    }
}

And here is my form, where i integrate the fieldset:

class FileForm extends AbstractForm implements InputFilterProviderInterface
{
    protected $fileFieldset;

    public function setFileFieldset(FileFieldset $fileFieldset): void
    {
        $this->fileFieldset = $fileFieldset;
    }

    public function init()
    {
        $this->add([
            'type' => FileFieldset::class,
            'options' => [
                'use_as_base_fieldset' => true,
            ],
        ]);

        $this->add([
            'type' => 'submit',
            'name' => 'submit',
            'attributes' => [
                'value' => 'los',
            ],
        ]);
    }

    public function getInputFilterSpecification()
    {
        return $this->fileFieldset->getInputFilterSpecification();
    }
}

If i debug my form in the controller, i see the right values that i set in the factory: debug output

No matter where I debug, I always see the expected values.

Why use laminas the initial values? Why do the initial values still exist?

[+++EDIT+++]

Interesting: I look at the object ID of the Fieldset in the function FileFieldset()->getInputFilterSpecification(). This function is called three times. The first two times with the expected values and the same object ID. The third time, the object ID is different and the values are the initial values.

Why is it not a singleton?

Why is there a FileFieldset instance that was not created with the factory?

FileFieldsetFactory:

class FileFieldsetFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $fieldset = new FileFieldset();
        $config = $container->get('config')['my_files'];
        $fieldset->setFileSizeMin($config['files_size_min']);
        $fieldset->setFileSizeMax($config['files_size_max']);
        $fieldset->setMimeTypes($config['files_mimetypes']);
        $fieldset->init();
        return $fieldset;
    }
}
bitkorn
  • 628
  • 1
  • 8
  • 22

1 Answers1

0

Two things that solve the problem:

  1. Set the name in the FileFieldset
    public function init()
    {
        $this->setName('file_fieldset');
  1. Add the FileFieldset instance in the FileForm, instead of with $this->add(['type' => FileFieldset::class]);
$this->add($this->fileFieldset);

So the object ID is always the same and the values as expected.

bitkorn
  • 628
  • 1
  • 8
  • 22