2016-05-15 80 views
0

如何使用Silex FormServiceProvider构建不使用树枝的表单?使用无树枝的Silex FormServiceProvider

我试过$form$form->createView(),我也查看了Form和FormView的API文档,这对我并不陌生,我该怎么做。

这里是我的代码:

$app->get('/form', function (Request $request) use ($app) { 
    // some default data for when the form is displayed the first time 
    $data = array(
     'name' => 'Your name', 
     'email' => 'Your email', 
    ); 

    $form = $app['form.factory']->createBuilder(FormType::class, $data) 
     ->add('name') 
     ->add('email') 
     ->add('billing_plan', ChoiceType::class, array(
      'choices' => array(
       1 => 'free', 
       2 => 'small_business', 
       3 => 'corporate' 
      ), 
      'expanded' => true, 
     )) 
     ->getForm(); 

    $form->handleRequest($request); 

    // I want return the form here 
    return 'ok'; 
}); 
+0

为了渲染一个表单,你需要一个[FormRenderer](https://github.com/symfony/symfony/blob/3.0/src/Symfony/symfony/blob/3.0/src/Symfony/Component/Form/FormRenderer.php)类的实例,依次使用[FormRendererEngine](https://github.com/symfony/symfony/blob/3.0/src/Symfony/Component/Form/FormRendererEngineInterface.php)。有一个[AbstractFormRendererEngine](https://github.com/symfony/symfony/blob/3.0/src/Symfony/Component/Form/AbstractRendererEngine.php),你可以尝试工作,但我强烈建议你使用Twig ,你将拥有开箱即用的一切 – mTorres

回答