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!
@Netzhexe, Just a quick guess…
Can’t you use a regular translation in the form definition?
Hmm, sure, I could. Doesn’t make a difference in this case since it’s definitely a German only project. But I don’t see how that would change my problem with altering the message AFTER the form has been submitted?
@Netzhexe, Sorry, too quick… I’ll have to rethink
What about the process.message
? You could have another page as described and show whatever message you want
@Netzhexe ,
Looking in the debugger at the definition of the Form object, it also seems to have a property ‘message’. Updating that field seems to work.
Try:
$event['form']['message'] = 'Your vote could not be saved, please try again';
$event['form']['status'] = 'error';
This will show the message in a red banner.
@anon76427325, once again, you’ve come to save my day! That did exactly what I wanted.
I did dump
the form object, but to be honest, I did not and still do not see how I could have known from its output that I would be able to make those changes, and that they would give me the result I wanted. I might have guessed, because of the field names. But I feel I don’t understand the debugger’s output very well, nice as it is. Is there an in-depth explanation of all the debugger formatting on the web somewhere, do you know?
Thanks again!
@Netzhexe, I could have saved you 3 days if you had payed attention 2 days ago…
I don’t believe the debugger or clockwork show the access modifiers (public, protected, private).
But the code does… See in plugin Form /plugins/form/classes/Form.php line: 66-71
Oh, you totally did! I was just too busy coding to reply straight away and I’ll look at that code as soon as I’m done with this super urgent thing. Thanks for the tip!