zend-frameworkzend-formzend-framework3zend-inputfilter

rename file which couldn't be imported using InputFilter


I'm not quite sure how to ask this properly. I import several files in one shot. The filenames include the matching systems where the files belong to. That works so far quite fine. But there will be files with no match, because the system is not found. I need to marker them, so the user can check.

My idea is to import this files also to the webserver but in a different directory. The directory could be shown to the user afterwards.

My InputFilter does the import to the right location which is for the matching files.

$inputFilter->add([
            'type'     => 'Zend\InputFilter\FileInput',
            'name'     => 'PAD_Document_Path',  // Element's name.
            'required' => true,    // Whether the field is required.
            'filters'  => [        // Filters.
                    [
                            'name' => \Zend\Filter\File\RenameUpload::class,
                            'options' => [
                                    'use_upload_name'=>true,
                                    'use_upload_extension' => true,
                                    'randomize' => false,
                                    'overwrite' => true,
                                    'target' => './public/files/pads',
                            ],
                    ],
            ],
            'validators' => [      // Validators.
                    [
                            'name' => \Zend\Validator\File\Extension::class,
                            'options' => [
                                    'extension' => 'pdf',
                                    'message' => 'File extension not match',
                            ],
                    ],
                    [
                            'name' => \Zend\Validator\File\MimeType::class,
                            'options' => [
                                    'mimeType' => 'application/pdf',    //'text/xls', 'text/xlsx',
                                    'message' => 'File type not match',
                            ],
                    ],
                    [
                            'name' => \Zend\Validator\File\Size::class,
                            'options' => [
                                    'min' => '1kB',  // minimum of 1kB
                                    'max' => '8MB',
                                    'message' => 'File too large',
                            ],
                    ],
            ]
    ]);

So can I use the InputFilter also to import the files with no match? If that is not possible, which way would be the best?

And other idea is to not import the not matching files at all, but then I would need to rename them at the clients system. That is probably not possible, isn't it?

I hope I explained it properly so everybody can understand.


Solution

  • Due to comments discussion below question, we came to a possible solution.

    (please give it a shot, else add your own answer when you solve it)


    if we go a step back, before importing. It is then the point of validation, if want to rename at this point, how would you do this?

    The docs show you the option for how to rename files, also by setting a target location for them. However, this is then a rule for all files.

    You should create a rule such as this, to uniformly validate the files. When you need files send to specific locations based on criteria, e.g. file names, then you should create this logic after the form validation. In a Controller class for example. In that Controller you handle the files matching the requirements in your "happy flow" and leave in the temporary those that aren't matched.

    If any files are not matched, you redirect the user to a location where he has to decide what the files are destined for and/or what to do with them. If there aren't any files you proceed as normal for your application's happy flow.

    Example flow:

    1. Files A through F are uploaded.
    2. A & B are fine, they're matched with, and moved for, systems X & Y.

    Alternate flow for files (C through F) not matched after 2 above

    1. Remainder cannot be matched.
    2. User gets redirected to page where he has to choose, per file, if the file is for X or for Y.
    3. Move the files as correction to selected options