[SOLVED] Form PHP $formModel->getElementData from calc

Status
Not open for further replies.

bea

Active Member
Hi,
I ran into an issue, which I don't understand.
I've a form PHP to write a new record into a second table with certain conditions.

The value of one of this elements (calc, condition) returns always a wrong value with var_dump (e.g. in database value=3 and in var_dump = 1). There are other elements with similar calc , which work correctly.

I've now changed the script from
PHP:
$losses3_rework_col =$formModel->getElementData('fab_cp1_line_tour___losses3_rework_col',true);
to
PHP:
$losses3_rework_col =$formModel->getElementData('fab_cp1_line_tour___losses3_rework_col'); 
$losses3_rework_col=is_array($losses3_rework_col) ? $losses3_rework_col[0]:$losses3_rework_col;
Why does this element need an array and the others not???
Cheers,
Bianka
 
Usually, elements which are based on the 'elementlist' model (which extends the base 'element' model), like joins, dropdowns, radiobuttons, checkboxes, etc will submit as an array. In general, those are elements which have multiple options (even if you can only select a single one, like a dropdown).

-- hugh
 
Note the "usually". Sometimes the data for those elements may not be an array, particularly when accessing them from the calc element (which might be running on submit, or it might be running on list load or details view load). Which is why you'll see a lot of my example code over the years use that is_array() thing.

-- hugh
 
I don't really get it, but will test in the future with both options...
Many thanks.
Bianka
 
Certain element types submit data as arrays, not single values. Like anything which renders as a dropdown - even if it's a single select dropdown, it could potentially be a multi-select. So the form submits the data as an array, ie. a name that looks like yourtable___yourelement[], with the square brackets on the end, and we simply expect the result to be an array, even if there's only going to be one value.

If it's a single select, you can just grab the first entry from the array, [0]. if it's a multi select, you have to handle dealing with multiple values in the array.

-- hugh
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top