How to check if a file is selected to upload in Laminas framework

150 Views Asked by At

I'm working on a laminas framework project and I want to know how to check if a file is selected to upload and display a message using an input filter.

The current code is as follows and when click on upload button without selecting a file it doesn't check for selected and directly checking the Extension.

        // file_upload
        $this->add([
            'name' => 'file_upload',
            'required' => true,
            'filters' => [],
            'validators' => [
                [
                    'name' => 'NotEmpty',
                    'options' => [
                        'message' => 'There is no file selected',
                        'break_chain_on_failure' => true
                    ]
                ],
                [
                    'name' => 'Laminas\Validator\File\Extension',
                    'options' => [
                        'extension' => 'csv',
                        'message' => 'Files that have the extension "%extension%" are only allowed',
                        'break_chain_on_failure' => true
                    ]
                ],
                [
                    'name' => 'Laminas\Validator\File\Size',
                    'options' => [
                        'min' => '10B',
                        'max' => '12Mb',
                        'message' => 'Cannot import the CSV file data. No data was found within the CSV file, or, the data was incomplete or invalid.',
                        'break_chain_on_failure' => true
                    ]
                ],
            ]
        ]);

I want to display a message when a user click on upload button without selecting a file.

How to fix this?

1

There are 1 best solutions below

0
Tharisha Perera On

The issue was in the 'name' => 'NotEmpty' field, the validator was incorrect and the correct validator was "Laminas\Validator\File\UploadFile" .