Question

I'm starting developing with Symfony2 and I have some problems with Forms. I used console to generate form fields based on my Entity. Everything looks great but I have to change labels of fields. When it comes to idCapacityUnit its based on another Entity and Symfony made that this field looks like "select". Now I want to change label of this field. So I used this code:

...
->add('idCapacityUnit', 'choice', array('label' => 'My label'))

but Symfony throws an error that I want to put wrong data type to it. Do I need to create another Data Transformer for this?

$builder
            ->add('nameShort', 'text', array('label' => 'Nazwa krótka'))
            ->add('capacityValue', 'text', array('label' => 'Pojemność'))
                  ->add('idCapacityUnit', 'choice', array('label' => 'Jednostka'))
            ->add('idCapacityUnit')
            ->add('priceDetail')
            ->add($builder->create('isPackage', 'checkbox')->addModelTransformer($transformer))
            ->add('lifeTime')
            ->add('components')
            ->add('isActive');

Maybe there is a way to put there something like that:

->add('idCapacityUnit', 'customEntityType:choice', array('label' => 'Jednostka'))

Also I don't know how to put label values in language file. I tried to:

  1. Create language file in path src\Vendor\NameOfBundle\Resources\translations\VendorNameOfBundle.en.yml
  2. Then in *Type class I wrote array('label' => 'form.labels.name_short')

in language file I have:

form:
    labels:
        name_short: "Nazwa krótka" 

then in browser I see form.labels.name_short...

Was it helpful?

Solution

Everything looks good but when it comes to language file you should rename it to messages.#you_lang_code#.yml. Then remember to refresh cache by typing:

php app/console ca:cl

When it comes to fields you may do as follow:

$builder
->add('idCapacityUnit', null, array('label' => 'form.lables.id_capacity_unit'))

Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top