Search code examples
gravity-forms-plugingrav

Is it possible to create 2 forms in one modular page using Grav CMS?


I am really new to Grav CMS. I am having a problem creating 2 forms (contact form and application/register form) in one modular page. Is it possible?


Solution

  • You can now do it with the Form 2.0 Plugin !

    And it is really simple, if you have for example two module on your modular page, in your case, contact form and register form.

    That mean you have two MarkDown files like this :

    contact_form.md :

    ---
    title: contact_form_modular
    forms:
        contact_form:
            name: contact
            action: /home
            fields:
                -
                    name: email
                    label: Email
                    validate:
                        required: true
            buttons:
                -
                    type: submit
                    value: Submit
            process:
                -
                    save:
                        fileprefix: register-
                        dateformat: Ymd-His-u
                        extension: txt
                        body: "{% include 'forms/data.txt.twig' %}"
                        operation: add
                -
                    message: 'Thank for contacting us !'
    ---
    
    ## Modular title
    
    Here a form to contact me
    

    And register_form.md :

    ---
    title: register_form_modular
    forms:
        register_form:
            name: register
            action: /home
            fields:
                -
                    name: email
                    label: Email
                    validate:
                        required: true
                -
                    name: password
                    label: Password
                    validate:
                        required: true
            buttons:
                -
                    type: submit
                    value: Submit
            process:
               - 
                    email:
                        from: "{{ config.plugins.email.from }}"
                        to: "{{ config.plugins.email.to }}"
                        subject: "Contact by {{ form.value.name|e }}"
                        body: "{% include 'forms/data.html.twig' %}"
                -
                    save:
                        fileprefix: register-
                        dateformat: Ymd-His-u
                        extension: txt
                        body: "{% include 'forms/data.txt.twig' %}"
                        operation: add
                -
                    message: 'Thank for registering'
    ---
    
    ## Modular title
    
    Here is a form to register to my app !
    

    Then in your contact_form.html.twig file in the template folder of your theme, you put the following line to render your form :

    {% include "forms/form.html.twig" with {form: forms('contact_form')} %}
    

    And the same for register_form.html.twig :

    {% include "forms/form.html.twig" with {form: forms('register_form')} %}
    

    Of course if you wish to, you can make your own form.html.twig to be able to customize the look of your form, you put the new file in a forms folder in your template folder of your theme.

    For example : template/forms/register_form_template.html.twig

    And in your register_form.html.twig you change like this :

    {% include "forms/register_form_template.html.twig" with {form: forms('register_form')} %}
    

    I hope I have been clear but in case here the link to the documentation :

    https://learn.getgrav.org/forms/forms#multiple-forms