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>
{{ nonce_field('admin-form', 'admin-nonce')|raw }}
</form>
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!