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

Submit form on admin page

Started by Muut Archive 9 years ago · 4 replies · 789 views
9 years ago

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' %}
<form id="{{ form_id }}" data-grav-form="{{ form_id }}" data-grav-keepalive="true">
{% for field in page.header.form.fields %}
{% if field.type %}
{% set value = data.value(field.name) %}
<div class="block block-{{field.type}}">
{% include ["forms/fields/#{field.type}/#{field.type}.html.twig", 'forms/fields/text/text.html.twig'] %}
</div>
{% endif %}
{% endfor %}
<input type="hidden" name="form-name" value="{{ form_id }}">
<div class="answer">
<button class="button primary">{{ "PLUGIN_GRAVITY_SUPPORT.SEND"|tu }}</button>
</div>

TWIG
{{ nonce_field('admin-form', 'admin-nonce')|raw }}

</form>

TXT

Page's .md file:

title: Answer
template: plugin_answer

access:
admin.super: true

form:
name: answer
validation: loose
method: post
action: answer

YAML
fields:
    - name: your_answer
      type: textarea
      id: your_answer
      autofocus: true
      validate:
        required: true
        label: Your answer
TXT

Plugin's php file:

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

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

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

 }

}

TXT


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!
9 years ago

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.

9 years ago

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:

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

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1349 9 years ago
Archive · by Muut Archive, 9 years ago
2 934 9 years ago
Archive · by Muut Archive, 9 years ago
2 4060 9 years ago
Archive · by Muut Archive, 9 years ago
1 2946 9 years ago
Archive · by Muut Archive, 9 years ago
3 1118 9 years ago