GravityKit / GravityView

The best and easiest way to display Gravity Forms entries on your website.
https://www.gravitykit.com/products/gravityview/
245 stars 63 forks source link

Edit Entry doesn't respect File Upload's Maximum Number of Files setting #2086

Open rafaehlers opened 1 month ago

rafaehlers commented 1 month ago

image

I was able to upload more files than what was set in Gravity Forms:

image

Bonus: When working on this bug, see if this can also be fixed in the same attempt: https://github.com/GravityKit/GravityView/issues/1002

rafaehlers commented 1 month ago

https://secure.helpscout.net/conversation/2659846004/56058

mrcasual commented 1 month ago

@omarkasem, please work on this.

zackkatz commented 1 month ago

@rafaehlers Please share any related snippets.

rafaehlers commented 1 month ago

Not exactly related, but deal with files uploaded on the edit entry page:

add_filter( 'gform_validation_30', 'custom_validation_filename', 11 ); // Change 30 to your form ID

function custom_validation_filename( $validation_result ) {
    $form           = $validation_result['form'];
    $uploaded_files = [];

    // Multi-file upload field.
    if ( ! empty( $_POST['gform_uploaded_files'] ) ) {
        $uploaded_files = json_decode( stripslashes( $_POST['gform_uploaded_files'] ), true );
    }

    // Single file upload field.
    if ( ! empty( $_FILES ) ) {
        foreach ( $_FILES as $input => $file_data ) {
            if ( isset( $uploaded_files[ $input ] ) ) {
                continue;
            }

            // Normalize single file uploads to match multi-file structure.
            $uploaded_files[ $input ] = [
                [ 'uploaded_filename' => $file_data['name'] ?? '' ]
            ];
        }
    }

    if ( empty( $uploaded_files ) ) {
        return $validation_result;
    }

    foreach ( $uploaded_files as $input => $files_data ) {
        $field_id = str_replace( 'input_', '', $input );
        $field    = GFAPI::get_field( $form, $field_id );

        if ( ! $field ) {
            continue;
        }

        foreach ( $files_data as $file_data ) {
            if ( ! preg_match( '/[¹²³–]/', $file_data['uploaded_filename'] ) ) {
                continue;
            }

            $validation_result['is_valid'] = false;
            $field->failed_validation      = true;
            $field->validation_message     = 'Nome do arquivo não deve conter caracteres especiais.';
        }
    }

    return $validation_result;
}
omarkasem commented 1 month ago

@mrcasual Please check the comments on the PR here https://github.com/GravityKit/GravityView/pull/2104

rafaehlers commented 2 weeks ago

Reopening because while we made some advances here, it's still not good enough.

Here's a new video explaining what's going on: https://www.loom.com/share/9956641a40334cd4b340a5cfcf8561de

We need to:

Correcting these two items above will prevent the bug shown in the video.

https://secure.helpscout.net/conversation/2659846004/56058