Saves label instead of value when "Show values" is enabled

Hello,

When “Show values” is enabled, the default behavior is to save the values instead of the labels.

I need the values for calculations, but I need the labels as well.

So I’ve succesfully written some lines of code to save the labels and the values in this format text : value

This is done by using this snippet :


add_action('gform_pre_submission', 'pre_submission_handler');
function pre_submission_handler($form)
{


        // Saves labels and values instead of values
        foreach ($form['fields'] as $field) {

                if ($field->type == 'checkbox') {

                        foreach ($field->choices as  $index => $choice) {
                                $value = $_POST["input_" . $field->id . "_" . ($index + 1)];
                                if ($choice['value'] == $value) {
                                        $_POST["input_" . $field->id . "_" . ($index + 1)] = $choice['text'] . " : " . $choice['value'];
                                }
                        }
                }

                if ($field->type == 'radio') {

                        foreach ($field->choices as  $index => $choice) {
                                $value = $_POST["input_" . $field->id];

                                if ($choice['value'] == $value) {
                                        $_POST["input_" . $field->id] = $choice['text'] . " : " . $choice['value'];
                                        break;
                                }
                        }
                }
        }
}

But here is my issue :

The reason I had enabled the “value” was to use them for calculation purposes within a “number field”.

It appears that using this snippet with the gform_pre_submission filter changes the values of those fields before the calculations take place.

Therefore, the calculation is messed up as it takes string instead of numbers as inputs.

Is there a way to make it work ?

Would it be possible to fire this function AFTER the calculation takes and to still save the labels instead of the value ?

Thanks for any help

EDIT : This doesn’t work fully for some fields, but I haven’t found out why yet.

I thought of a work around, this might work for you too if you use the number fields for calculations only.

If you happened to use a “number field” as an input, you should probably exclude it from this code :


add_action('gform_pre_submission', 'pre_submission_handler');
function pre_submission_handler($form)
{
        // for each field, if the field type is equal to number, 
        // then save the value in an array named 'numbers'
        $numbers = array();
        foreach ($form['fields'] as $field) {
                if ($field['type'] == 'number') {
                        $numbers[$field->id] = $_POST["input_" . $field->id];
                }
        }

        // Saves labels and values instead of values
        foreach ($form['fields'] as $field) {

                if ($field->type == 'checkbox') {

                        foreach ($field->choices as  $index => $choice) {
                                $value = $_POST["input_" . $field->id . "_" . ($index + 1)];
                                if ($choice['value'] == $value) {
                                        $_POST["input_" . $field->id . "_" . ($index + 1)] = $choice['text'] . " : " . $choice['value'];
                                }
                        }
                }

                if ($field->type == 'radio') {

                        foreach ($field->choices as  $index => $choice) {
                                $value = $_POST["input_" . $field->id];

                                if ($choice['value'] == $value) {
                                        $_POST["input_" . $field->id] = $choice['text'] . " : " . $choice['value'];
                                        break;
                                }
                        }
                }
        }


        // for each field, if the field type is equal to number, 
        // then retrieve the value from the 'numbers' array
        // and set the value of the field to the value of the array element

        foreach ($form['fields'] as $field) {
                if ($field->type == 'number') {
                        $_POST["input_" . $field->id] = $numbers[$field->id];
                }
        }
}

Doing so is not supported and it would break multiple features that would expect the value to be the one you have set for the field choice in the field settings and not text : value

So your best bet is to not alter the value being saved and consider a different approach for your goal. If you provide us further details of why you decided to do this maybe we can point you into the right direction.

Thanks for answering,

I’ve actually seen a few similar topics on the community board who remained unanswered until now,
so your answer is really appreciated.

As said in my first post :

To give more informations about my use case,
let’s imagine I do have a checkbox like this one :

“Which meal can you cook ?”
A - Lasagna (Value : 1)
B - Paella (Value : 1)
C - Chicken Thai Curry (Value : 1)
D - Sous Vide Cooked French style Lamb with homemade ratatouille (Value : 2)

My form also contains plenty of other fields looking like this one (more than 40).
Some of the values might be negatives as well.

The form is used as a “Quizz” for the visitor, and as a survey for me.

So it gives the user a score based on the values of all the selected fields.
And when looking at the entry, or when passing the datas, I should be able to learn about my customers.

You’re welcome @jonathanpak

The following snippet can be used to show choices labels instead of values, it’s a display only solution so it doesn’t alter the value saved: https://docs.gravityforms.com/gform_entry_field_value/#2-display-choice-label

If you elaborate what you mean with that maybe I can point you to another filter.