Problem capitalizing characters when posting input data [RESOLVED]

I signed up with demo license before purchasing. I came across this issue while inspecting form structures. I couldn’t see an option to capitalize all characters in the input data. When I was doing research on this subject, one of the internet users mentioned a code added to the function.php file below.

add_action('gform_pre_submission_1', 'capitalize_fields_1');
    function capitalize_fields_1($form){
            // add all the field IDs you want to capitalize, to this array
            $fields_to_cap = array('input_1');
		               foreach ($fields_to_cap as $each) {
                    // for each field, convert the submitted value to uppercase and assign back to the POST variable
                    // the rgpost function strips slashes
                                       $_POST[$each] = strtoupper(rgpost($each));
            }
            // return the form, even though we did not modify it
            return $form;
    }


and CSS text transform code:

text-transform: uppercase;

But this code doesn’t quite work for me. Because in keyboard entries in Turkey, when the lowercase letter “i” is capitalized, it turns into the letter “I”, that is, different characters from each other. I want to convert the wrong letter “I” to the letter “İ”.

By searching Turkish sources, I found a code that solves this problem. However, I could not adapt it to the above function.php file.

function mb_strtoupper_tr($metin){
    $metin=str_replace('i', 'İ', $metin);
    return mb_strtoupper($metin,'UTF8');
}

How can I resolve this situation?

@arifemreeryilmaz2 An adapted version of the capitalization for Turkish would look something like this…

<?php

/**
 * Replace special characters and run capitalization posted values.
 * 
 * @param array $form Form object.
 */
add_action( 'gform_pre_submission_1', function( $form ) {

	// Inputs we want to convert.
	$inputs = [ 'input_1' ];

	// Replacement dictionary. Array key is the original, array value is the replacement.
	$dictionary = [
		'i' => 'İ',
	];

	// Loop through defined inputs.
	foreach ( $inputs as $input ) {

		// If no value was posted for input, skip it.
		if ( ! ( $value = rgpost( $input ) ) ) {
			continue;
		}

		// Replace the dictionary values in the posted value.
		$value = str_replace( array_keys( $dictionary ), array_values( $dictionary ), $value );

		// Replace posted value with capitalized string.
		$_POST[ $input ] = mb_strtoupper( $value, 'UTF8' );

	}

} );

I added a new $dictionary array that you can use to add proper conversions for multiple wrong letters.

2 Likes

leonardo-dicaprio-clapping

Thank you for your help. I solved this problem with the code below. However, your code seems to be more efficient.

add_action('gform_pre_submission_1', 'capitalize_fields_1');
function capitalize_fields_1($form){
// add all the field IDs you want to capitalize, to this array
$fields_to_cap = array('input_1');
        foreach ($fields_to_cap as $each) {
                // for each field, convert the submitted value to uppercase and assign back to the POST variable
                // the rgpost function strips slashes
                
                $_POST[$each] = strtoupper(str_replace('i', 'İ', rgpost($each)));
        }
        // return the form, even though we did not modify it
        return $form;
}

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.