Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rafaehlers opened this issue Jul 23, 2024 · 7 comments · Fixed by #2104 or #2132
Closed

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

rafaehlers opened this issue Jul 23, 2024 · 7 comments · Fixed by #2104 or #2132
Assignees
Milestone

Comments

@rafaehlers
Copy link
Contributor

rafaehlers commented Jul 23, 2024

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: #1002

@rafaehlers
Copy link
Contributor Author

@mrcasual mrcasual added this to the 2.27 milestone Aug 8, 2024
@mrcasual
Copy link
Collaborator

mrcasual commented Aug 8, 2024

@omarkasem, please work on this.

@zackkatz
Copy link
Member

@rafaehlers Please share any related snippets.

@rafaehlers
Copy link
Contributor Author

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
Copy link
Collaborator

@mrcasual Please check the comments on the PR here
#2104

mrcasual added a commit that referenced this issue Aug 16, 2024
- This solves #2086
- The edit entry validation for max files works but after the user
clicks edit entry.
- The issue here is the files are uploaded to the uploads folder before
they are validated
- Gravity forms already has a good ajax validation for this part but we
have a filter that removes that validation in GravityView
`add_filter( 'gform_plupload_settings', array( $this,
'modify_fileupload_settings' ), 10, 3 );`
- It says that it was added because our custom validation already
handles that so we don't have a conflict ( I tried to find that conflict
but couldn't)
- So we can leave it as it is and we would have ajax validation (gf
validation) and also a custom validation after the user clicks update
(Our validation)
- Or we can keep the filter that removes gf validation and build our
own.
@rafaehlers
Copy link
Contributor Author

rafaehlers commented Sep 4, 2024

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:

  • Find out how many files have already been uploaded, and if the limit has already been reached, block the upload button on the page load.
  • Only show the "Maximum number of files reached" message after uploading the latest file that reached the limit. If the limit is only one file, that message ideally doesn't need to appear.

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

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

@rafaehlers
Copy link
Contributor Author

Customer notified ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment