Custom field to add a custom template to a form

2,444 views
Skip to first unread message

jonben

unread,
Sep 21, 2012, 8:40:50 AM9/21/12
to sonata...@googlegroups.com
Sorry i'm new to Symfony2 and i don't get much about twig. I'm stuck on something quite simple i guess but i can't find how to do it.

I would like to insert some custom text in a edit form, between 2 fields.

Here for exemple, inside the form i would like to insert a div right under the "highseason" input in which i would display with javascript in real time the high season price = price * highseason%.

  protected function configureFormFields(FormMapper $formMapper)
  {
    $formMapper
      ->add('price')
      ->add('highseason', 'percent')
      ->add('lowseason', 'percent') 
      ->end;
  }

I cannot find a way to insert a custom field in the form that wouldn't be a field but just a text zone with a template and no field type or validation needed.

Is something like that possible?

  protected function configureFormFields(FormMapper $formMapper)
  {
    $formMapper
      ->add('price')
      ->add('highseason', 'percent')
      ->add('highseasoncalcul', null, array( 'template' => 'MyMainBundle:Admin:high-season-calcul .html.twig')) 
      ->add('lowseason', 'percent') 
      ->add('lowseasoncalcul', null, array( 'template' => 'MyMainBundle:Admin:low-season-calcul .html.twig')) 
      ->end;
  }

I remember doing such things in Symfony 1.4 admin generator, adding partials to forms.

jonben

unread,
Sep 21, 2012, 8:49:32 AM9/21/12
to sonata...@googlegroups.com
Also I managed to add custom fields in lists this way

    $listMapper
      ->add('price')
      ->add('highseason', 'percent')
      ->add('lowseason', 'percent') 
      ->add('custom', 'string', array('template' => 'MyMainBundle:Admin:prices.html.twig'))
    ;

The same line in configureFormFields brings a "Could not load type "string""  exception, and null as type doesn't work either.

Thomas R.

unread,
Sep 24, 2012, 4:33:51 AM9/24/12
to sonata...@googlegroups.com
It is not possible to use the template parameter for form. You need to create custom block from as explained in the Symfony documentation (template and form). you can provide default form template by extending the getFormTemplates method.
Reply all
Reply to author
Forward
0 new messages