Hello everybody,
I have a simple form that saves a single radio button choice. This gets written to a file, and a success message is displayed. However, just in case something goes wrong, I would like to modify the success message if the file could not be written to (I created a plugin to handle the form processing). I have tried this:
public function onFormProcessed(Event $event)
{
$form = $event['form'];
$action = $event['action'];
$params = $event['params'];
switch ($action) {
case 'savetocsv':
// create string from input
$data = "";
$data .= $this->grav['user']['fullname'].";";
$data .= $this->grav['user']['votes'].";";
$data .= $form->value('choice').";";
$data .= time().";";
// append string to csv file
$locator = $this->grav['locator'];
$path = $locator->findResource('user-data://', true);
$filename = $path . DS . 'votes.csv';
$written = file_put_contents($filename, $data, FILE_APPEND);
if ($written) { // something went wrong, change success message
$form['process'][1]['message'] = "Your vote could not be saved, please try again";
}
}
}
This produces a Crikey! error that says "Indirect modification of overloaded element of Grav\Plugin\Form\Form has no effect" for the line in the if statement.
Makes sense, but how do I do it then?
Thank you for your ideas!