• Resolved standaman22

    (@standaman22)


    Hey! I was wondering if there was a way to access the file before it is uploaded so i can make a custom error message by reading the CSV file before the user submits the form?

    My wish is to just ensure there is only one email in each row and I am currently unable to get access to the file_path in $csv_team_upload using the forminator_custom_form_submit_errors hook and was wondering if there was a better way to achieve this goal? I have pasted the code the whole code below for reference.

    add_filter('forminator_custom_form_submit_errors', function( $submit_errors, $form_id, $field_data_array ) {
    	$formatted_data = array();
    	$min_additional_users = 2; // does not include the main account which is created separately
    	
    	foreach ( $field_data_array as $field ) {
    		$field_name  = isset( $field['name'] ) ? $field['name'] : '';
    		$field_value = isset( $field['value'] ) ? $field['value'] : '';
    		$formatted_data[$field_name] = $field_value;
    	}
    
    	$csv_team_upload = isset($formatted_data['upload-2']) ? esc_url($formatted_data['upload-2']['file']['file_path']) : null;	
    
    	if($csv_team_upload) {
    		$csv_file_contents = file_get_contents($csv_team_upload);
    		$csv_array = str_getcsv($csv_file_contents, "\n");
    		$emails_array = array();
    		$count_valid_emails = 0;
    
    		// loop through each row
    		error_log("CSV array: " . print_r($csv_array, true));
    		foreach($csv_array as $csv_row){
    			error_log("CSV Row: " . print_r($csv_row, true));
    			$email_columns = 0; //counts how many email addresses exist in each row
    			$csv_columns = str_getcsv($csv_row, ',');
    
    			// loop through each row cell 
    			foreach($csv_columns as $column){
    				error_log("Column: " . print_r($column, true));
    				// detecting cells which contain an email address
    				$email = filter_var(trim($column), FILTER_VALIDATE_EMAIL);
    				if($email) { // cell contains an email
    					$email_columns++; // increment counter
    					if($email_columns > 1){ //more than one field has email
    						// show selector modal
    						 $submit_errors[]['upload-2'] = __( "Your CSV includes $email_columns columns containing email addresses. Please modify your file to include one column of email addresses, then re-upload.");
    					} elseif($email_columns < 1) {
    						$submit_errors[]['upload-2'] = __( "Your CSV does not include any email addresses. Please modify your file to include one column of email addresses, then re-upload." );
    					}
    // 					array_push($emails_array,$email);
    					$emails_array[$count_valid_emails] = $email;
    					$count_valid_emails = $count_valid_emails + 1;
    				}
    			}
    		}
    		if($count_valid_emails < $min_additional_users) {
    			$submit_errors[]['upload-2'] = __( "Your CSV includes fewer than $min_additional_users Team Members (" . $count_valid_emails . "). You must register at least $min_additional_users additional Team Members to qualify for a Team Subscription." );
    			error_log(print_r($emails_array, true));
    		}
    	}
    	error_log("Errors: " . print_r($submit_errors, true));
    	
    	$submit_errors[]["upload-2"] = __("Fail please.");
    	return $submit_errors;
    }, 10, 3 );

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @standaman22

    I hope you are doing well today.

    I pinged our SLS Team to review your query and see how complex this will be and is there anything we can do within the scope of the support in that matter. We will post an update here as soon as more information is available.

    Kind Regards,
    Kris

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi again @standaman22

    After review with SLS, it seems we have some bug reported in that matter, and future updates should fix this. From what I checked this should be this week. Once the new version is available you will be able to get the path in?$csv_team_upload

    Kind Regards,
    Kris

    Thread Starter standaman22

    (@standaman22)

    Thanks for looking into it so quickly Kris. I’ll be looking forward to your update.
    All the best mate.

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @standaman22 ,

    The fix for this bug is in Forminator 1.29.0 and it was already released.

    If you need more assistance let us know.

    kind regards,
    Kasia

    Thread Starter standaman22

    (@standaman22)

    Thank you for the update

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Accessing file for custom error before submission’ is closed to new replies.