Search code examples
formssymfonytooltip

TranslatorInterface errors, can't use tooltips in form that is used in another


I am trying to create tooltips for my addressType. I have the following for that.

use Symfony\Component\Translation\TranslatorInterface;



class AddressType extends AbstractType{
   /**
 * @var TranslatorInterface
 */
private $translator;


 /**
 * @param TranslatorInterface $translator
 */
 public function __construct(TranslatorInterface $translator)
{
    $this->translator = $translator;
}

public function buildForm(FormBuilderInterface  $builder, array $options){
    $builder->add(
            'country','text', array(
            'label' => 'address.country',
            'translation_domain' => 'messages' ,
            'required' => true,
            'attr' => array('class'=>"tooltipped",
             'data-position' =>"bottom", 
             'data-delay '=>"50",
             'data-tooltip' => $this->translator->trans('help.tooltips.address.country'))

I have done this multiple times for other forms and it works. However the thing is my addressType is being used for another form named customerType, which also houses tooltips and translator as well. the tooltips i use there work fine. But when I add the addresstype it crashes

->add(
            'address', new AddressType(),array( // line 84
            'label'=>false,
            'required' => false,

The error I get is as followed:

Catchable Fatal Error: Argument 1 passed to AppBundle\Form\AddressType::__construct() must implement interface Symfony\Component\Translation\TranslatorInterface, none given, called in C:\Users\KevinDeLeeuw\Documents\GitHub\mountguru\src\AppBundle\Form\CustomerType.php on line 84 and defined

My questions how do I resolve this, or is such a thing not allowed?


Solution

  • You create new AddressType object in your custom form, instead of this pass form name (< v2.8) or class name AddressType::class (for symfony 2.8+)

    ->add('address', AddressType::class, [...])
    

    And of course you need to define your address form type as service Defining your Forms as Services