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.
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.