How to get labels instead of values for a checkbox in a form's original data?

EricWebsite

Member
I have a form where on submission I compare the original data with the current (possibly updated) data.
I mark the updates with a color and mail them to an administrator.

That works well for normal fields. But for checkboxes the output in the original data colum looks like: ["1","2","10","11","5","7","8"] while in the current content it is shown as a nice bullet list of the Labels.

How can I extract the labels from the $origData as well? My code:

PHP:
<?php
$origData = $formModel->getOrigData();

$fieldtobecompared = array ('tablename___initials', 'tablename___firstname', 'tablename___lastname', 'tablename___username', 'tablename___email', 'tablename___street', 'tablename___number', 'tablename___postalcode', 'tablename___city', 'tablename___volunteertasks');

// volunteertasks is the checkbox element
echo '<table><tr><td>Field</td><td>Previous value</td><td>New value</td></tr>';
foreach ($fieldtobecompared as $field) {
    $original = $origData[0]->$field;
    $current = $this->data[$field];
    if ($original != $current) {
        $color = 'red';
    } else {
        $color = 'black';
    }
    echo '<tr><td style = "color: ' . $color . ';">' . str_replace ('tablename___', '', $field) . '</td>';
    echo '<td>' . $original . '</td><td>' . $current . '</td></tr>';
}
echo '</table>';
?>
Any ideas? Thanks in advance!
 
Hi troester,

Thanks a lot! That indeed works. I added a check on the field type, so that my comparison to find modified fields works for both normal fields and arrays, like checkboxes, dropdowns, radiobuttons, etc. Here is the full code in case other people might need it:
PHP:
<?php
$origData = $formModel->getOrigData();

$fieldtobecompared = array ('tablename___initials', 'tablename___firstname', 'tablename___lastname', 'tablename___username', 'tablename___email', 'tablename___street', 'tablename___number', 'tablename___postalcode', 'tablename___city', 'tablename___volunteertasks');
// volunteertasks is the checkbox element

echo '<table><tr><td>Field</td><td>Previous value</td><td>New value</td></tr>';

foreach ($fieldtobecompared as $field) {

    $original = $origData[0]->$field;
    $current = $this->data[$field];
  
    $original_values = json_decode($original,true);
    if (is_array($original_values)) {
        $elementModel = $formModel->getElement($field);
        $original_labels = $elementModel->getEmailValue($original_values, $formModel->formDataWithTableName, 0);
        $original = $original_labels;
    }
  
    if ($original != $current) {
        $color = 'red';
    } else {
        $color = 'black';
    }
  
    echo '<tr><td style = "color: ' . $color . ';">' . str_replace ('tablename___', '', $field) . '</td>';
    echo '<td>' . $original . '</td><td>' . $current . '</td></tr>';
}
echo '</table>';
?>
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top