1084

file validation helper hitting die statement

Comments for “file validation helper hitting die statement”
 

Posted by frocco on Tuesday 7th June 2022 at 10:02 GMT

I have this statement
$this->validation_helper->set_rules('myfile', 'file', 'required|validate_file|allowed_types[txt,csv,pdf]|max_size[20000]');
$result = $this->validation_helper->run();

I tried removing validate_file, but still get the die statement.
It looks like the required is not getting executed in the validation helper because the file_validation_helper is executed first.

die('ERROR: Invalid validation rule.');

It works if I remove the die statement in the file_validation_helper and make the following changes in the validation_helper.
// $posted_value = $_FILES[$key];
//$tests_to_run[] = 'validate_file';
$posted_value = $_FILES[$key]['name'];
$tests_to_run = $this->get_tests_to_run($rules);

frocco

User Level: Guest

Date Joined: 7/10/2021

Posted by Davcon on Friday 10th June 2022 at 17:48 GMT

I read three times and could not understand precisely what the nature of your problem is.

Have you tried generating an image uploader with the desktop app and then looking at the code that is generated?

This comment was edited by Davcon on Friday 10th June 2022 at 17:49 GMT

Founding Member

Davcon

User Level: Founding Member

Date Joined: 3/11/2018

Posted by DaFa on Sunday 12th June 2022 at 02:40 GMT

Hi frocco,

I don't understand the logic in your rules you are trying to check > 'required' & 'validate_file'.

On line 9 of the validation_helper:
if ((!isset($_POST[$key])) && (isset($_FILES[$key]))) {

As the super global '$_FILES' is set, the default is to check 'validate_file' first, by setting
$test_to_run[] = 'validate_file'

So there is no need to check for 'validate_file' in your rules, and 'required' will always be true, because $_FILES[$key]['name'] is set.

Here is the full method (with your 2 lines commented out):
    public function set_rules($key, $label, $rules) {

        if ((!isset($_POST[$key])) && (isset($_FILES[$key]))) {

            if (!isset($_POST[$key])) {
                $_POST[$key] = '';
            }

            $posted_value = $_FILES[$key];
            $tests_to_run[] = 'validate_file';
            // $posted_value = $_FILES[$key]['name'];
            // $tests_to_run = $this->get_tests_to_run($rules);
        } else {
            $posted_value = $_POST[$key];
            $tests_to_run = $this->get_tests_to_run($rules);
        }

        $validation_data['key'] = $key;
        $validation_data['label'] = $label;
        $validation_data['posted_value'] = $posted_value;

        foreach ($tests_to_run as $test_to_run) {
            $this->posted_fields[$key] = $label;
            $validation_data['test_to_run'] = $test_to_run;
            $this->run_validation_test($validation_data, $rules);
        }
        $_SESSION['form_submission_errors'] = $this->form_submission_errors;
    }


so even if you force the rules to:
[
    "required",
    "validate_file",
    "allowed_types[txt,csv,pdf]",
    "max_size[20000]"
]

by uncommenting the two lines above and commenting the 2 original lines. The run_validation_test() method will check 'required' first from the $test_to_run() array and the 'check_for_required' method will always be true as the file name from $posted_value will never equal "".
    private function check_for_required($label, $posted_value) {

        $posted_value = trim($posted_value);
        die($posted_value);
        if ($posted_value == '') {
            $this->form_submission_errors[] = 'The ' . $label . ' field is required.';
        }
    }

Also, commenting out line 109 of the file_validation_helper.php
die('ERROR: Invalid validation rule.');
is just short circuiting '$file_check_key' as 'validate_file' is not being checked, only ['allowed_types', 'max_size', 'max_height', 'max_width', 'min_height', 'min_width', 'square'] are being checked.

So your solution is to restore the validation_helper.php & file_validation_helper.php to their original states and remove 'required|validate_file' from your rules as they are being checked by default, and adding them to the rules is only going to break your code.

Try this:
$this->validation_helper->set_rules('myfile', 'file', 'allowed_types[txt,csv,pdf]|max_size[20000]');
$result = $this->validation_helper->run();


Cheers,
Si

This comment was edited by DaFa on Sunday 12th June 2022 at 02:59 GMT

Founding Member

DaFa

User Level: Founding Member

Date Joined: 30/11/2018

Posted by frocco on Monday 25th July 2022 at 14:46 GMT

Thank you DaFa for taking the time to correct my logic.

frocco

User Level: Guest

Date Joined: 7/10/2021

Posted by DaFa on Monday 25th July 2022 at 23:19 GMT

You're welcome Frocco,

If you could please close off this thread and the others left open, that would be appreciated.
Founding Member

DaFa

User Level: Founding Member

Date Joined: 30/11/2018

×