grav-plugin-form icon indicating copy to clipboard operation
grav-plugin-form copied to clipboard

empty json message - ajax

Open danielkoptak opened this issue 4 years ago • 2 comments

Hi, if I submit the form through ajax, which passes successfully, then I get back an empty json message: ajax:

$.ajax({
             type: form.attr('method'),
             url: form.attr('action'),
             data: form.serialize(),
             dataType: 'json',
 
             success: function (result) {
                 console.log(result);
                 form.find('input:text, input[type=email], textarea').val('');
                 $('#form-modal .content').html(result.message);
             },
             error: (error) => {
                 console.log(JSON.stringify(error));
                 $('#form-modal .content').html(error.statusText);
             }
         });

form-messages.json.twig

{% set inline_errors = form.inline_errors is not null ? form.inline_errors : config.plugins.form.inline_errors(false) %}
{% set message = inline_errors and form.messages ? "GRAV.FORM.VALIDATION_FAIL"|t : form.message %}
{% if form.status == "success" %}
    {% set message = "PLUGIN_FORM.POSITIVE_MESSAGE"|t %}
{% endif %}
{{ {'status':form.status, 'message':message}|json_encode|raw }}

the form is processed, email will be sent but the result is empty. The problem occurred after upgrading from version 4.3.1 to 5.0.0 Thank you for any help...

danielkoptak avatar Feb 25 '21 10:02 danielkoptak

There's no lang string PLUGIN_FORM.POSITIVE_MESSAGE which is why you get an empty message. The default file has:

{% set inline_errors = form.inline_errors is not null ? form.inline_errors : config.plugins.form.inline_errors(false) %}
{% set message = inline_errors and form.messages ? "GRAV.FORM.VALIDATION_FAIL"|t : form.message %}
{{ {'status':form.status, 'message':message}|json_encode|raw }}

It seems you have your own form-messages.json.twig ? You need to provide a translation string in that case.

rhukster avatar Mar 12 '21 20:03 rhukster

@danielkoptak I had the exact same problem. Getting back an empty server response or content of the debugbar.js. After adding the 'message' attribute to the frontmatter again, the logic in my custom form-message.html.twig was running again:

    process:
        -
            email:
                from: '{{ config.plugins.email.from }}'
                to:
                    - '{{ form.value.subject|e }}'
                subject: 'Anfrage {{ form.value.name|e }}'
                body: '{% include ''mail/contact.html.twig'' %}'
            display: /kontakt/danke
        -
            message: 'hello folks'

tecmec avatar Mar 15 '22 10:03 tecmec