Help with $_POST vs $this->data

How to set default values for empty file upload fields in ChronoForms.

Overview

The issue occurs because ChronoForms copies $_POST data into its internal $this->data array before actions run, so modifying $_POST directly does not update the data used by subsequent actions.
Instead of modifying $_POST, update the $this->data array directly using either $this->data["key"] = "value" or $this->data("key", "value", true) to set default placeholder values for empty fields.

Answered
ChronoForms v6
he healyhatman 23 Jun, 2017
Please help my, I'm tearing my hair out here it's been literally HOURS trying to get this to work. I have another form on another site where it works perfectly.

I have a form where someone needs to upload 1 or 2 photos - not 0. I have two separate file upload fields, "certificatesRSA" and "certificatesWhiteCard".

I can perform a check to make sure at least one image has been attached:
$hasNoRSA = empty($this->data("certificatesRSA", ""));
$hasNoWC = empty($this->data("certificatesWhiteCard", ""));

//Return true if both RSA and WC are empty
return ($hasNoRSA && $hasNoWC);


It's messy but it's what I got working after a lot of troubleshooting.

NOW: since I'm using a TCPDF action, if the person only uploads one image then the other one needs to be replaced with a placeholder image so that TCPDF doesn't fail.

if(empty ($this->data("certificatesRSA", "")))
  $_POST["certificatesRSA"] = "noCertificate.png";

if (empty($this->data("certificatesWhiteCard", "")))
{
     $_POST["certificatesWhiteCard"] = "noCertificate.png";
}


This same sort of thing works 100% on another site - I can get and set the value of the field in question using $_POST but I CANNOT FUC*&^ING GET IT TO WORK on this site! when I try to
echo $_POST[field name]
even the populated field I get NOTHING, even though the debug action shows there's something there.

PLEASE HELP ME I CAN'T GO TO SLEEP TIL I GET IT WORKING AND IT'S 11PM

Here's what works perfectly fine on the other site:
if(empty($_POST['photoIDAttachment']))
 $_POST['photoIDAttachment'] = "noID.png";
he healyhatman 23 Jun, 2017
DEBUG ACTION:

Array
(
    ................
    [certificatesRSA] => pexels-photo.jpg
    [certificatesWhiteCard] => pexels-photo.jpg
    ................
)


var_dump($_POST)

Array
(
    ................
    [certificatesRSA] => 
    [certificatesWhiteCard] => 
    ................
)

Gr GreyHead 23 Jun, 2017
Hi healyhatman,

ChronoForms copies the contents of $_POST into $this->data when the form is submitted before any actions run so changing the $_POST values after that will not change the data that CF is using. If you use the equivalent $this->data entries it should be OK.

Bob
Max_admin Max_admin 23 Jun, 2017
Answer
Hi,

The values can be set in the data array using:

$this->data["key"] = "value"; //method #1
$this->data("key", "value", true); //method #2


Best regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
he healyhatman 24 Jun, 2017
Thanks for that, I didn't see that anywhere in the guides.
This topic is locked and no more replies can be posted.