Accessing the label in a dropdown element using formData['organisation']

Status
Not open for further replies.

alisamii

Member
Hi

I am using a PHP plugin on OnAfterProcess to populate another table in the database. It needs to be able to access the label element of the dropdown, but I am only able to access the value element of the dropdown. I have tried using:
PHP:
$organisation_label = $formModel->formData['organisation'];
$organisation_value = $formModel->formData['organisation_raw'][0];

The $organisation_label is always blank and looking in the array this is because it only contains the value.
I have even tried creating a hidden element and updating it with the data selected in the dropdown and posting that to my table, but that is also blank.

For example the label I am trying to access is - "Organisation for Economic Co-operation and Development (OECD), France" and the value is - "oecd-france"

Thanks

Ali
 
That's because during form processing, the labels typically aren't loaded. They don't get submitted with the HTML form data, which is an HTML thing not a Fabrik thing, as the form will only submit the value="foo" part of the <option value="foo"...> for a dropdown, or a radio or a checkbox.

So the only way we can know what the labels are, is by loading the params for that element from our metadata tables. Which is an expensive operation, so we only do it if we have to. Which we don't have to when just saving a submission, as we're only writing the value, not the label, to the database.

So we have a form model method to do it, which is then called by any plugins that specifically want label data, like the form email plugin. The form method then iterates through all the elements, calling a per-element method to get the label. In fact, the individual element method to get the label from the value is called getEmailValue(), as that was originally the only thing that needed to do this (many years ago).

So ... if you only need to get the formatted label for a single element, you can call the getEmailValue() method for just that element. Try something like this:

Code:
$organisation_value = $formModel->formData['organisation_raw'][0];
$elementModel = $formModel->getElement('yourtable___organization');
$organization_label = $elementModel->getEmailValue($organization_value, $formModel->formDataWithTableName, 0);

You'll need to change 'yourtable' to, well, your table name, to get the full name for that element.

-- hugh
 
  • Like
Reactions: rob
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top