Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silex + Twig: form_widget() not found, even with Symfony Bridge installed

I've been following the documentation at the resource below

http://silex.sensiolabs.org/doc/providers/form.html

But I get the following error

Twig_Error_Syntax: The function "form_widget" does not exist in "layout.html.twig"

I've checked the documentation and my source multiple times, I still can't see where I'm going wrong, what am I missing? I have the Symfony Twig Bridge installed.

<?php
require_once __DIR__.'/vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Yaml\Parser;

use Silex\Provider\FormServiceProvider;

$app = new Silex\Application();
$app['debug'] = true;

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path'     => __DIR__. '/src/views',
    'twig.options'  => array(
        'debug' => true,
        'cache' => false
    ),
));

$app['twig']->addExtension(new Twig_Extension_Debug());

$app['yaml'] = new Parser();



//.....

$app->register(new FormServiceProvider());

$app->register(new Silex\Provider\ValidatorServiceProvider());



$site['name']  = 'My Site';
// More definitions for $site

$app->match('/', function() use($app, $site) { 
    return $app['twig']->render('layout.html.twig', $site);
})->method('POST|GET');


$app->match('/form/', function(Request $request) use($app, $site) { 

    $data = array(
        'name' => 'Your name',
        'email' => 'Your email',
    );

    $form = $app['form.factory']->createBuilder('form', $data)
        ->add('name')
        ->getForm();

    if ('POST' == $request->getMethod()) {
        $form->bindRequest($request);

        if ($form->isValid()) {
            $data = $form->getData();

            // do something with the data - I haven't got that far yet

            // redirect somewhere
            return $app->redirect('/');
        }
    }

    $site['form'] = $form->createView();

    return $app['twig']->render('form.html.twig', $site);

})->method('POST|GET');

$app->run();

EDIT: the composer.json file

{
    "require": {
        "silex/silex"       : "dev-master",
         "symfony/config"   : "dev-master",
         "symfony/yaml"     : "dev-master",
         "doctrine/common"  : ">=2.1,<2.3-dev",
         "doctrine/dbal"    : ">=2.1,<2.3-dev",
         "symfony/dependency-injection": "dev-master",
         "symfony/console"  : "dev-master",
         "monolog/monolog"  : ">=1.0.0",
         "twig/twig"        : ">=1.2.0",
         "symfony/form"     : "2.1.*",
         "symfony/translation": "2.1.*",
         "symfony/twig-bridge": "2.1.*",
         "symfony/validator": "2.1.*"
    }
}
like image 532
Adam Elsodaney Avatar asked Jan 16 '23 07:01

Adam Elsodaney


1 Answers

You need to register the form provider first, twig provider after that.

The twig provider checks if the form provider is registered, and only enables the form-related twig extensions if neccessary.

like image 199
Maerlyn Avatar answered Jan 25 '23 16:01

Maerlyn