Submit form on admin page

Hi,
I created a new admin page for my plugin and I need to save some datas on file after submit.

My form template:
— html
{% set form_id = form_id ? form_id : ‘answer’ %}
{% include ‘partials/messages.html.twig’ %}

{% for field in page.header.form.fields %} {% if field.type %} {% set value = data.value(field.name) %}
{% include ["forms/fields/#{field.type}/#{field.type}.html.twig", 'forms/fields/text/text.html.twig'] %}
{% endif %} {% endfor %}
{{ "PLUGIN_GRAVITY_SUPPORT.SEND"|tu }}
{{ nonce_field('admin-form', 'admin-nonce')|raw }}
```

Page’s .md file:

title: Answer
template: plugin_answer

access:
    admin.super: true

form:
    name: answer
    validation: loose
    method: post
    action: answer
    
    fields:
        - name: your_answer
          type: textarea
          id: your_answer
          autofocus: true
          validate:
            required: true
            label: Your answer

Plugin’s php file:

public static function getSubscribedEvents()
{
   return [
       'onPluginsInitialized' => ['onPluginsInitialized', 0],
       'onFormProcessed' => ['onFormProcessed', 0]
   ];
}

public function onFormProcessed(Event $event)
{
    $action = $event['action'];

    switch ($action) {

         case 'answer':
               
                 $this->grav->fireEvent('onFormValidationError', new Event([
                        'form'    => $form,
                        'message' => $this->grav['language']->translate('This is an error')
                 ]));
                 $event->stopPropagation();
                 return;
               
         break;
                
     }
}

I just wanted to submit this form and retrieve a custom error, just to see if the function works properly.
But every time, I get the same error:
Oops there was a problem, please check your input and submit the form again.

What am I doing wrong?

Isn’t the case name same as the action name?

Thanks in advance!

No one can help me?

This is a bit more complicated than on the frontend. You can use the onAdminSave event to handle special saving from the admin, but I don’t have a good example right now. I hope to release some more information including a sample plugin soon.

Ok, I’ll try that function.

Well, I tried onAdminSave but no success.

On plugin’s main class, I’ve added this event to subscriptions.

As with onFormProcessed, nothing happens. It seems that these events are not triggered.

Just to test, I’ve added to subscriptions onFormValidationError, and added to the function the code I need on onFormProcessed and…it works.

Just like this:

public function onFormValidationError(Event $event)
    {
        $form = $event['form'];
        
        if (isset($event['message'])) {
            $form->message_color = 'red';
            $form->message = '<i class="fa fa-exclamation-triangle"></i> '.$event['message'].$_POST['admin_answer'];
            $form->messages = $event['messages'];
        }
 
        $admin = 1;
            $answer = filter_var(urldecode($_POST['admin_answer']), FILTER_SANITIZE_STRING);

            $admin_name = $user->fullname;
         

            $file_location = DATA_DIR . 'myplugin/file.yaml';
            $file = File::instance($file_location);

            if (file_exists($file_location)) {
                $data = Yaml::parse($file->content());

                $data['answers'][] = [
                    'admin' => $admin,
                    'sender' => $admin_name,
                    'text' => $answer,
                    'files' => 0,
                    'date' => date('D, d M Y H:i:s', time()),
                ];

                $file->save(Yaml::dump($data));
            }    

        $event->stopPropagation();
    }

With this I’m sure that my function works properly, at least. In fact, after submitting the form the function adds a new row on the specified file, but only on onFormValidationError and not on onFormProcessed or onAdminSave.