symfony2 form entity add values - php

I just need to add other defaults options to an "entity field" in Symfony2. It displays names, and I need an option "Name not in list", but I cant find a way to achieve it. Data transformers cant seem to fix my problem though.
$builder
->add('family', 'entity', array(
'class' => 'InterneFichierBundle:Family',
'property' => 'Name'
))
If the name of the family is not in the list, there should be an option "name not in list"..
Thanks a lot !

i'm pretty sure you can just specify an empty value option:
$builder
->add('family', 'entity', array(
'class' => 'InterneFichierBundle:Family',
'property' => 'Name',
'empty_value' => 'Name not in list',
))
see http://symfony.com/doc/current/reference/forms/types/entity.html#empty-value

You shouk try with :
for information you can see it here : http://symfony.com/fr/doc/current/reference/forms/types/entity.html
$builder->add('users', 'entity', array(
'class' => 'AcmeHelloBundle:User',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.username', 'ASC');
},
));

Related

add option to select tag for EntityType field before creating the form

I have a form containing two EntityType fields, first one is category and the second is subCategory, the category field will have all the categories that i have in the database, but i want to add one more to these, which is "all categories", and i want it to be the one selected by default, how can i achieve this..
$builder
->add(
'category',
EntityType::class,
[
'label' => 'Catégorie',
'class' => Category::class,
'choice_label' => 'title',
'mapped' => false
]
)
->add(
'subCategory',
EntityType::class,
[
'label' => 'Sous-catégorie',
'class' => SubCategory::class,
'choice_label' => 'title',
'mapped' => false
]
);
You can construct your array of choices manually and use ChoiceType, for example:
// retrieve list of your categories in your controller or somewhere else
// and pass it using the options. Add it also before the default option "All categories".
$categories = $options['categories'];
$builder->add('category', ChoiceType::class, [
'choices' => $categories,
...

cant translate name UploadButton in my Form

i got a problem in my form. Im translating all my fields with message.yml and it works. But i have also a upload button from VichUploaderBundle in it. I can translate the label, but when im testing it, the label is in english, but the button is in german.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('id', TextType::class, array('disabled' => true))
->add('title',TextType::class, array('label' => 'edit.title'))
->add('body', TextareaType::class, array('label' => 'edit.body'))
->add('date', DateType::class, array('disabled' => true, 'label' => 'edit.date'))
->add('tags', EntityType::class, array(
'class' => 'AppBundle:Tag',
'choice_label' => 'getTitle',
'multiple'=> true,
'expanded'=> true))
->add(
'technology',
EntityType::class,
array(
'class' => 'AppBundle\Entity\Technology',
'choice_label' => 'getTitle',
'group_by' => 'parent.getTitle',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('t')
->where('t.parent IS NOT NULL')
->andWhere('SIZE(t.children) <= 0');
}
))
->add('imageFile', VichImageType::class, array(
'required' => false,
'allow_delete' => true,
'download_link' => true,
'label' => 'edit.image_file',
))
;
}
I dont know how to translate the button.
You are only creating a html file input.
This input does not have some label attribute.
<input type="file" name="imageFile"/>
-> the button label is set by your browser (which seems to be in german).
You can try to change it with CSS/JS
These text is defined by the browser and not by PHP or Symfony. You will also notice that this widget will be displayed differently in different browsers and on different operating systems. There is also no possibility to change the design of the widget. Most CSS properties won't have an effect on .
The only thing that you could do is to implement a custom JavaScript uploader, but that will probably only work in modern browsers.
I have not never done this, but a quick Google search brought up http://www.queness.com/post/11434/7-javascript-ajax-file-upload-plugins that features few JavaScript plugins to do this.

Adding a default value along with entity in form type of Symfony2

I am a totally new in symfony2. I met a situation which I can't solve till now.
I have a controller, formtype and eventsubscriber in my project. The form builds by binding the entities. In that case for a particular entity I need to add a default value along with an ID in one of the form fields. My form type is
$builder->add('breed', EntityType::class, array(
'label' => 'Breed',
'class' => 'AppBundle:Masters\Breed',
'placeholder' => '----Select Breed----',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('b')
->orderBy('b.sortOrder', 'DESC');
},
'choice_label' => 'breed',
));
$builder->addEventSubscriber(new BreedSubscriber($factory));
My Event Subscriber is
private function addBreed($form, $breedmasterId) {
$form->add($this->factory->createNamed('breed',EntityType::class,null, array(
'class' => 'AppBundle:Masters\Breed',
'placeholder' => '----Select Breed--------',
'choice_label' => 'breed',
'required' => TRUE,
'mapped' => false,
'query_builder' => function (EntityRepository $repository) use ($breedmasterId) {
$qb = $repository->createQueryBuilder('bm')
->where('bm.breed = :breedmasterId')
->setParameter('breedmasterId', $breedmasterId);
return $qb;
},
'auto_initialize' => false
)));
}
I need to add a default value such as "General" along with an "id" in the addBreed subscriber and it need to be validate on formsubmission. Thanks in advance for the help.
You can add the following 'data' key in the array you configure your form:
$builder->add('breed',null, array(
'label' => 'Breed',
'data' => 'Sih Tzu'
))

Use collection as entity with type in Symfony2 form

Is it possible to generate select list where options will be retrieved from database and there will be option (with JS) to multiply field ?
I've copied my code, maybe it will help.
$builder->add('device', 'collection', array(
'type' => 'entity',
'label' => ' ',
'options' => array(
'class' => 'TrashTrashAdminBundle:DeviceType',
'property' => 'name',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('d')
->where('d.isLighting = 1');
},
),
'attr' => array('class' => 'form-control device')
))
You are trying to work with collection field type in way as you would work with entity field type. You should change collection for entity:
$builder->add('device', 'entity', array(
'options' => array(
'class' => 'TrashTrashAdminBundle:DeviceType',
'property' => 'name',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('d')
->where('d.isLighting = 1');
},
),
'attr' => array(
'class' => 'form-control device'
))
);
After the select box is generated, you can make changes, which you mentioned, at your will.

add more than one property in a form symfony2

I have a form type where I will add an entity and add more than one attribute
$builder ->add('number', 'entity', array(
'class' => 'TelnOperatorBundle:Numberrange',
'property' => 'De'
'multiple' => false,));
In the property I want to add another field ie
$builder ->add('number', 'entity', array(
'class' => 'TelnOperatorBundle:Numberrange',
'property' => 'De','A'
'multiple' => false,));
How could I do it?
->add('subnumbers', 'collection', array('type' => new SubnumberType(),
'allow_add' => true,
'allow_delete' => true))
You can't, property only allows a string as a parameter: http://symfony.com/doc/master/reference/forms/types/entity.html#property
The solution in your case would be to leave the property blank and then implement the __toString() method in your Entity.
First change the way you define your form type (pass a blank value to property):
$builder ->add('number', 'entity', array(
'class' => 'TelnOperatorBundle:Numberrange',
'multiple' => false,));
Second, define the __toString() function in your TelnOperatorBundle:Numberrange entity:
public function __toString()
{
return $this->De . ' : ' . $this->A;
}

Categories