Entity Type is defined as follow in my form:
->add('esame_' . $i, EntityType::class, array(
'label' => false,
'mapped' => false,
'class' => 'AppBundle:Nome_esame',
'required' => true,
'multiple' => true,
'choice_label' => 'nome',
// 'disabled' => 'disabled',
'attr' => array(
'placeholder' => 'Esami',
'class' => 'max_width esame_row select_esame',
// 'class'=>'col-md-12 col-md-offset-0 col-xs-9 col-xs-offset-3 ',
)
))
In my form submit, I want to take all the selected value and create a new entity from every one.
I tried this:
foreach($form->get('esame_0')->getData() as $value){
$field= new Field();
$field->setvalue($value); // ->$value i want is the val of selected option
}
but $form->get('esame_0')->getData() does not return the selected data..
How can I do it?
The value that comes with EntityType is an Entity object. Try this way.
$entityObject = $form->get('esame_0')->getData()
$data = $entityObject->getId() or $entityObject->(Entity getter function)
Related
It is even possible to make ?
->add('product', CollectionType::class, [
'entry_type' => EntityType::class, array(
'data' => $options['product'],
'placeholder' => 'Wybierz klienta',
'multiple' => true,
'class' => Products::class,
'attr' => ['class' => 'chosen-select','data-placeholder'=>'Wybierz produkt'],
'choice_label' => function ($product) {
return ''.$product->getJson()["products"]["name"] .' | Stan Magazynowy: '.$product->getJson()["products"]["stock"].'';
},
'label' => 'Wybierz produkty'
),
'entry_options' => [
'label' => 'Value',
],
'label' => 'Add, move, remove values and press Submit.',
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'required' => false,
'attr' => [
'class' => 'my-selector',
],
])
I'll try to add chosen list of Products::class in CollectiontType, if some one wanna add product to new order, can add new EntityType and select product, and after submit i`ll handle this as array and save it to order.
If someone has another idea how to add products to form with quantity and then post it to array, please free to write :)
I think this is not the best way to do that.
You just have to add CollectionType to the FormType and update you ProductType class to handle if this is selected or not. May be you have to create a custom formtype for products for order only :-/ . All hard work are on the javascript side
I have a search form that gets Categories from database and shows each category name as a checkbox, it's retrieved via EntityType, i want to show count categories in front of the category name, i managed to count each one from the database and put the result in an array like this : $categCount[name] = count;
How can i show in front of each category name her count using EntityType ?
->add('categories', EntityType::class, ['class' => 'AppBundle:Categorie',
'choice_label' => 'name',
'multiple' => true,
'expanded' => true,
'required' => false,
])
Now. I`m not sure how your entity looks like but if you have the count in the same entity you can do something like:
->add('categories', EntityType::class, ['class' => 'AppBundle:Categorie',
'choice_label' => 'getNameWithCategoriesCount',
'multiple' => true,
'expanded' => true,
'required' => false,
])
and in your entity
public function getNameWithCategoriesCount(){
return $this->getName().' '.$this->getCount();
}
Alternatively(not tested) you can :
->add(
'categories',
EntityType::class,
[
'class' => 'AppBundle:Categorie',
'choice_label' => function ($category) use ($this-em) {
return $category->getDisplayName() $this->em->getRepo..()->getCount($category);
},
'multiple' => true,
'expanded' => true,
'required' => false,
]
)
NOTE: For this approach you need to declare your form as service and pass the entityManager
I have the following form:
$form = $this->createFormBuilder()
->setMethod('POST')
->add('users', EntityType::class, array(
'class' => 'AppBundle:User',
'choices' => $users,
'expanded' => true,
'multiple' => false,
'choice_label' => function ($user) {
return $user->getUsername();
}
))
->add('selected', SubmitType::class, array('label' => 'select'))
->getForm();
return $this->render('default/showUsers.html.twig', array('form' => $form->createView()));
I have 2 Problems with that:
I can not customize the 'choice_label' like:
'choice_label' => function ($user) {
return ($user->getId() + " " + $user->getUsername());
}
There is not Linebreak after each choice (or after each Radio button), which gets pretty ugly with the alot of users.
How can I customize the 'choice_label's ?
How can I get a Linebreak after each Radio button ?
You can customise this to string method however you want and then remove the 'choice_label' attribute in form builder
//in user entity
public function __toString()
{
$string =$this->getId(). ' ' . $this->getUsername();
return $string;
}
To customise labels you, I would use style sheets. You can add a class using attr or choice_attr for individual radio inputs based on their values .. For example
->add('users', EntityType::class, array(
'class' => 'AppBundle:User',
'choices' => $users,
'attr' => array('class' =>'type_label'),
'choice_attr' => array(
'0' => array('class' => 'class_one'),
'1' => array('class' => 'class_two'),
),
'expanded' => true,
'multiple' => false,
))
See symfony reference for more information
I`ve got a dropdown list in my Symfony2 form like this:
$builder->add('categories','entity', array(
'class' => 'MyBundle:Myentity',
'property' => 'name',
'label' => 'Mylabel',
'expanded' => false,
'multiple' => false,
'label_attr' => array ( 'class' => 'control-label' ),
'attr' => array ( 'class' => 'form-control',
'placeholder' => 'Placeholder',
'title' => "Mytitle",
'data-toggle' => 'tooltip',
'data-myidfromDB' => '????',
'data-mynamefromDB'=>'????' etc. )));
So I am getting a list of MyBundle:Myentity objects and when I choose one I want to show all its properties (like ID, name, etc.) which are stored in my DB and described in Entity class, in different html data-* fields. If I select another one from the list I want to see all information related to my newly selected option in HTML (to change dynamically). Any ideas how to do that?
Since Symfony 2.7 you can set the option choice_attr to ChoiceType and set it a callable receiving the choice as an argument.
EntityType inherits this option and the choice in that case is the instantiated entity, so you can write something like :
$builder->add('categories','entity', array(
'class' => 'MyBundle:MyEntity',
'property' => 'name',
'label' => 'Mylabel',
'attr' => array('class' => 'form-control'),
'label_attr' => array('class' => 'control-label'),
'choice_attr' => function (\AppBundle\Entity\MyEntity $myEntity) {
return array(
'data-private-property' => $entity->getPrivateProperty(),
'data-some-value' => $entity->someMethod(),
);
},
);
You can't do that in easy way.
But you can put more information in select label.
Look on
http://symfony.com/doc/current/reference/forms/types/entity.html#choice-label
Yout can put here more field details and get it from your javascript.
I am having a problem using symfony2 and a classless form with a choice field.
The setup is very, very simple:
$data=array();
$choices=array(1 => 'A thing', 2 => 'This other thing', 100 => 'That other thing');
$fb=$this->createFormBuilder($data);
$fb->add('mythings', 'choice', array(
'choices' => $choices,
'data' => $data,
'required' => false,
'expanded' => true,
'multiple' => true,
'mapped' => false))->
add('save', 'submit', array('label' => 'Save'));
$form=$fb->getForm();
And then...
$request=$this->getRequest();
$form->handleRequest($request);
if($form->isValid())
{
//Go nuts.
}
It is in the "go nuts" part where I am stuck. I actually need to access the selected choices by their original index as given in the $choices array... And I haven't been able to. This is the closest I got:
$submitted_data=$form->get('mythings');
foreach($submitted_data as $key => $value)
{
echo $key.' -> '.$value->getData().'<br />';
}
Which assuming we mark the first and last choices, yields:
0 -> 1
1 ->
2 -> 1
It is absolutely necessary that this be a classless form. How do I access the original index (in our case, 1, 2 or 100) of the choices?.
Thanks a lot.
$form->get('mythings')->getData()
will give you data that you need.
I hope this helps.
It's an issue with field mapping. the data you're passing into the form needs to be applied to the field you're adding ('mythings') so you have to wrap $data in an array as a value to the key 'mythings'.
For example:
$data=array();
$choices=array(1 => 'A thing', 2 => 'This other thing', 100 => 'That other thing');
$fb=$this->createFormBuilder(array('mythings' => $data));
$fb->add('mythings', 'choice', array(
'choices' => $choices,
'required' => false,
'expanded' => true,
'multiple' => true))
->add('save', 'submit', array('label' => 'Save'));
$form=$fb->getForm();
you could try naming the array keys to preserve the actual index
$data=array();
$choices=array('1' => 'A thing', '2' => 'This other thing', '100' => 'That other thing');
$fb=$this->createFormBuilder($data);
$fb->add('mythings', 'choice', array(
'choices' => $choices,
'data' => $data,
'required' => false,
'expanded' => true,
'multiple' => true,
'mapped' => false))->
add('save', 'submit', array('label' => 'Save'));
$form=$fb->getForm();