2
votes

I am trying to validate a file upload as below using a custom validation rule but its not working.

class JFormRuleResume extends JFormRule
{
  public function test(&$element, $value, $group = null, &$input = null, &$form = null)
  {
    jimport('joomla.filesystem.file');
    jimport('joomla.filesystem.folder');
    $jinput = JFactory::getApplication()->input;
    $fileInput = new JInput($_FILES);
    $file = $fileInput->get('jform', null, 'files', 'array');
    //$files = $jinput->files->get('jform');
    //$file = $files['resume'];

    $filename = JFile::makeSafe($file['name']['resume']);
    $filesize = $file['size'];

    if (strtolower(JFile::getExt($filename))!='pdf') {
        $element->addAttribute('message', strtolower(JFile::getExt($filename)));
        return false;
    }
    if($filesize<2000000){
        $element->addAttribute('message', "File size bigger than 2MB");
        return false;
    }
    //var_dump($files);
    return true;

  }
}

Whether I upload a pdf file or other files with different extension, the error "Invalid file type" is returned.

Please enlighten me what the problem is?

My field looks like this:

<field
        name="resume"
        type="file"            
        label="Resume"
        description=""
        size="40"
        accept="application/pdf"
        validate="resume"
        required="true"
    />

UPDATE

This is an update to what i've discovered so far. The post data

$requestData = JRequest::getVar('jform', null, 'post', 'array');

do not fetch the file input value. It is therefore, i've to add the following code in the controller action before i validate the form $data = $model->validate($form, $requestData);

    $requestData = JRequest::getVar('jform', null, 'post', 'array');
    // Get the file data array from the request.
    $jinput = JFactory::getApplication()->input;
    $fileInput = new JInput($_FILES);
    $file = $fileInput->get('jform', null, 'files', 'array');

    // Make the file name safe.
    $filename = JFile::makeSafe($file['name']['resume']);
    $requestData['resume'] = strtolower($filename);
    JRequest::setVar('jform', $requestData );

    $form   = $model->getForm();

    if (!$form) {
        JError::raiseError(500, $model->getError());
        return false;
    }

    $data   = $model->validate($form, $requestData);

This way i am able to inject the file input value to the post array. But the problem still lies somewhere because the above validation works only partially. The validation works correctly only for files like docx, png, htm, php, txt .... and if i submit doc, pdf (larger than 2MB), zip files the validation do not work at all, instead i am displayed back the form without data and with the validation warning that all the fields are missing.

This is very strange, i desperately need some help.

1
Turn on your debugger and tell us what JFile::getExt($filename) returns? My guess is, something on the way (i.e. $filename, $file or $fileInput) is not what you expect.Markus Malkusch
which debugger do you mean? Joomla debug console is onraaman
What do you get with var_dump(JFile::getExt($filename));? Also, change != to !==Lodder
I see nothing returned by var_dump(). I changed it to !== also. Infact when i add required="true" for the field in the form it gives me "Field Required: Resume" and then when i remove it, i get "Invalid Field: Resume" ..... I don't see the custom validation working at all nowraaman
Can you please try adding var_dump('test'); just before return true; and if you don't see anything, I think I might know what the problem is.Lodder

1 Answers

0
votes

Finally made through with the problem of form being redisplayed when file of bigger size is uploaded. It was a server problem, had to change the following in php.ini of my wamp server

upload_max_filesize = 100M
post_max_size = 100M

And regarding the second issue for file upload value not being in post was a new thing to me which i didn't know. Had to upload the file seperately first, then get the name by which the file was saved and then use that name to be inserted in the database.