Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Forms & Blueprints

How do I modify the confirmation message after form submit?

Solved by pamtbaau View solution

Started by Anna 5 years ago · 8 replies · 745 views
5 years ago

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:

PHP
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!

5 years ago

@Netzhexe, Just a quick guess...

Can't you use a regular translation in the form definition?

  • In the form: message: CONTACT_FORM.THANK_YOU
  • And in the 'languages.yaml' file:
    YAML
    en:
    CONTACT_FORM:
      THANK_YOU: Thank you for your message.
    de:
    CONTACT_FORM:
      THANK_YOU: Danke für deine Nachricht.
    
5 years ago

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?

5 years ago

@Netzhexe, Sorry, too quick... I'll have to rethink

last edited 01/28/21 by pamtbaau
5 years ago Solution

@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:

PHP
  $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.

last edited 01/28/21 by pamtbaau
5 years ago

@pamtbaau, once again, you've come to save my day! :-D 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!

5 years ago

@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

5 years ago

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!

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1145 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 67 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 142 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 118 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 136 7 months ago