Question

I have looked through similar answers but couldn't find any solution.

I have a file element in one of my forms. This element is defined below.

$file = $this->createElement('file', 'templateFile');
$file->setAttrib('id', 'templateFile')
     ->setAttrib('class', 'form-control')
     ->setAttrib('placeholder', 'File containing template headers')
     ->setLabel('Template File <span class="glyphicons glyphicons-sheriffs_star requiredFlag"></span>')
     ->setRequired(true)
     ->addValidator('NotEmpty', false)
     ->addValidator('Count', false, 1)
     ->addValidator('Size', false, '1MB')
     ->addValidator("Extension", false, array('csv', 'txt'))
     ->addFilter('StringTrim');

$file->getValidator('NotEmpty')->setMessage('Please select a template file');
$file->getValidator('Count')->setMessage(' Please upload one file at a time');
$file->getValidator('Size')->setMessage('File exceeds the upload limit of 1 MB');
$file->getValidator('Extension')->setMessage('Invalid file extension. Only CSV and TXT files are allowed');

I am using custom error messages with this element and this is what is causing problems.

When I submit a form without choosing a file rather then triggering the NotEmpty validation error message I see the message below.

File 'qhepstow_dblive.sql' exceeds the defined form size

How can I trigger the following error message when the file is not selected and avoid the once mentioned above?

This error message should be displayed when the file is not selected.

Please select a template file

Thanks for your help.

Was it helpful?

Solution

I managed to solve this problem. Hope it helps someone in the future.

The problem was with the sequence in which I was adding the validators to the file elemenet.

Changing the sequence to below solved the problem.

$file = $this->createElement('file', 'templateFile');
        $file->setAttrib('id', 'templateFile')
            ->setAttrib('class', 'form-control')
            ->setAttrib('placeholder', 'File containing template headers')
            ->setLabel('Template File <span class="glyphicons glyphicons-sheriffs_star requiredFlag"></span>')
            ->setDestination($destination)
            ->setRequired(true)
            ->addValidator('Count', false, 1)
            ->addValidator('NotEmpty', false)
            ->addValidator('Size', false, '15MB')
            ->addValidator("Extension", false, array('txt' ,'csv'))
            ->addFilter('StringTrim');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top