Using Symphony Forms Component with Grav

It would be cool if we could use Symfony’s forms component with Grav, in order to create more flexible forms.
Right now, I’m just trying to get it to work in a theme, but in the future there will be a standalone ‘advanced forms’ plugin.
So Far
composer require symfony/form

In the theme’s main file
require_once __DIR__ . '/vendor/autoload.php';

I’m following the examples in the Symfony Book: https://symfony.com/doc/master/book/forms.html

Importing stuff

use Symfony\Component\Form\Forms;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

This runs when the onTwigSiteVariables event fires:

$formFactory = Forms::createFormFactory();
    
$form = $formFactory->createBuilder()
  ->add('task', TextType::class)
  ->add('dueDate', DateType::class)
  ->add('save', SubmitType::class, array('label' => 'Create Task'))
  ->getForm();

And a form object actually gets created. Hooray!

The Question

How do I actually render the form using:

{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}

…in one of the theme’s template files?

You need to expose those functions to Twig. For example see how the Comments plugin exposes the comments: https://github.com/getgrav/grav-plugin-comments/blob/develop/comments.php#L56-L64 or how Admin adds Twig filters (a similar approach is for Twig functions): https://github.com/getgrav/grav-plugin-admin/blob/develop/twig/AdminTwigExtension.php - https://github.com/getgrav/grav/blob/develop/system/src/Grav/Common/Twig/TwigExtension.php.

That said, Grav has a Form Plugin that could be extended with proposals to make it more powerful and flexible. What do you miss in it now?

I’ll check out those examples.
There are two features that would make the Form plugin really great:

  1. The ability to output a form anywhere in a page (maybe with something like a short code, or twig function)
  2. Being able to add arbitrary HTML between the form inputs

You could use a display or spacer field to output some text. Not sure it accepts any HTML, need to check.

Add a form anywhere is something missing. You can add this as an issue on https://github.com/getgrav/grav-plugin-form/issues to keep track of possible enhancements.