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

Modular Forms

Started by Muut Archive 10 years ago · 6 replies · 1404 views
10 years ago

Hi.

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

10 years ago

It will be very possible, even without modular pages very shortly. If you are feeling adventurous you can test this out in the mutliple-form branch: https://github.com/getgrav/grav-plugin-form/tree/feature/multiple_form_handling

With this you will be able to define multiple forms in a page called `/form':

YAML
forms:
    form-a:
        fields:
            ...
         buttons:
            ...
        process:
            ...

    form-b:
        fields:
            ...
         buttons:
            ...
        process:
            ...

Then to output a form you would simply do this in your Twig:

TWIG

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

{% include "forms/form.html.twig" with {form: forms('/form', 'form-b')} %}
---
10 years ago

I initialized my form in modular.md page

forms:
form-a:
action: /your-modular-page
name: application_form
fields:

  • YAML
            name: name
            label: name
            placeholder: 'Full name'
            type: text
            validate:
                required: true
    
  • YAML
            name: email
            label: email
            placeholder: 'Your email'
            type: email
            validate: 
                required: true
    
  • YAML
            name: company_name
            label: company_name
            placeholder: 'Company Name'
            type: text
            validate:
                required: true
    buttons:
    
  • TWIG
            type: submit
            value: Submit
            body: "{% include 'forms/data.txt.twig' %}"
    
    process:
    
  • email:
    from: "{{ config.plugins.email.from }}"
    to: "{{ form.value.email }}"
    subject: "Contact by {{ form.value.name|e }}"
    body: "{% include 'forms/data.html.twig' %}"
  • TXT
            message: 'Thank you for your feedback!'
    

    form-b:
    action: /your-modular-page
    name: contact_form
    fields:

  • YAML
            name: name
            label: name
            placeholder: 'Full name'
            type: text
            validate:
                required: true
    
  • YAML
            name: email
            label: email
            placeholder: 'Your email'
            type: email
            validate:
                required: true
    

and include my file

{% include "forms/applicationform.html.twig" with {form: forms('/form', 'form-a')} %}

but it's not working. It returns a blank page. Do have any idea? Maybe I miss something.

10 years ago

I downloaded the multiple_form_handling on the link you sent me and now I got this error

Notfound

here is my modular.html.twig content

--- html

{% extends 'partials/base.html.twig' %}

{% block content %}
{{ page.content }}
{% for module in page.collection() %}
{{ module.content }}
{% endfor %}
{% endblock %}


10 years ago

It doesn't have error now but the 2 forms is not working. Is it okay to put the form in modular.md, under the modular pages? Form-a is working/displaying but form-b doesn't.

10 years ago

I tested with modular pages and it worked fine for me. Make sure you clear the cache between each change because Twig caching for modular pages is very aggressive.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1362 9 years ago
Archive · by Muut Archive, 9 years ago
2 939 9 years ago
Archive · by Muut Archive, 9 years ago
2 4069 9 years ago
Archive · by Muut Archive, 9 years ago
1 2959 9 years ago
Archive · by Muut Archive, 9 years ago
3 1124 9 years ago