10

I have managed to setup the symfony forms to work as standalone in my project. However i can only get it to work with twig. Is it possible for me to render the forms without twig?

The way i currently do it is:

#Controller
echo $twig->render('index.html.twig', array(
    'form' => $form->createView(),
));

#Twig File
{{ form_widget(form) }}

Is it possible to render the form without twig?

Any help is greately appreciated

1
  • Hi, can you accept my answer? Commented Aug 23, 2018 at 10:07

2 Answers 2

3

It is hard to find resources about how to use Symfony's form component 1) without Twig and 2) without Symfony.

I guess this is not recommended but I made this up for myself as a first starting point:

<?php

require 'vendor/autoload.php';

use Symfony\Component\Form\Forms;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;

$form = Forms::createFormFactory()->createBuilder()
    ->add('firstname', TextType::class, ['label' => 'My firstname'])
    ->add('age', IntegerType::class, ['label' => 'My age'])
    ->getForm();

$form->handleRequest(); // Looks into superglobals and detects if the user submitted the form, if so it submits the form with $form->submit()

if ($form->isSubmitted() && $form->isValid()) {
    $data = $form->getData();
    var_dump($data);
}

$formView = $form->createView();

?>

<html>
    <body>
        <form
            action="<?php echo $form->getConfig()->getAction(); ?>"
            method="<?php echo $form->getConfig()->getMethod(); ?>"
        >
            <?php foreach ($formView->getIterator() as $field) { ?>

                <div>

                    <label
                        for="<?php echo $field->vars['id']; ?>"
                    >
                        <?php echo $field->vars['label']; ?>
                    </label>

                    <input
                        type="<?php echo $field->vars['block_prefixes'][1]; ?>"
                        id="<?php echo $field->vars['id']; ?>"
                        name="<?php echo $field->vars['full_name']; ?>"
                        value="<?php echo $field->vars['value']; ?>"
                    />

                    <?php if ($field->vars['required']) { ?>
                        <span>This field is required</span>
                    <?php } ?>

                </div>

            <?php } ?>

            <input type="submit" />
        </form>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

2

First you have to go to app/config and check if php is included as templating engine

templating:
    engines: ['php', 'twig']

And here is one of the ways to render form with php:

echo $view['form']->form($form,array('attr' => array('class' => 'Form')));

There are plenty of examples for rendering forms in the symfony2 official site. You can render field by field or the full form as shown i my example.

4 Comments

I am using the standalone library i found here (github.com/bschussek/standalone-forms). He is explicity loading the twig library in a setup.php file. Can i not render the form without a templating engine?
My suggestion is to use the default form builder of symfony2 because it is very well documented and you can find very easy in the forums answers to your problems.
Author ask how to use the "Standalone" form component without Symfony framework, the your $view['form']-> is the Symfony Helper...
Others methods than form are availables like start, widget, row, end : api.symfony.com/4.0/Symfony/Bundle/FrameworkBundle/Templating/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.