Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Symfony forms without Twig

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

like image 683
Sajuna Fernando Avatar asked Dec 11 '13 12:12

Sajuna Fernando


2 Answers

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>
like image 100
Michael Käfer Avatar answered Sep 29 '22 23:09

Michael Käfer


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.

like image 38
gprusiiski Avatar answered Sep 29 '22 23:09

gprusiiski