Symfony2 error when trying to create entity field in form - forms

$tip->setGame($em->getRepository('XXXBundle:Game')->find($id));
$form = $this->createFormBuilder($tip)->add('player', 'entity', array(
'class' => 'XXXBundle::FootballPlayer',
/*'query_builder' => function(\XXX\XXXBundle\Entity\FootballPlayerRepository $er)
{
$er->findByCurteam($team->getName());
},*/
))->getForm();
(not really using 'XXX' in my code)
error:
Warning: class_parents(): Class XXX\XXXBundle\Entity\ does not exist
and could not be loaded in
D:\www\xxx\xxx\vendor\doctrine\lib\Doctrine\ORM\Mapping\ClassMetadataFactory.php
line 223
seems the Entity class is not found - strange

Something is strange in your code: 'class' => 'XXXBundle::FootballPlayer', are you sure :: exist? Never seen it, seems like a mistake (maybe can provoke the error).
After testing, yes, it's because of the double :: replace by :: 'class' => 'XXXBundle:FootballPlayer',.

Related

Object provided to Escape helper, but flags do not allow recursion on hidden field

Okay this is a weird one to me...Usually i get this error when there's a problem with the date/time format, but it's throwing up on a hidden field, even though it's set to view formhidden...Has this happened to anyone before?
That's the view:
echo $this->formHidden($steppedEdge->get('glassSections'));
This is the form fieldset:
$this->add([
'name' => 'glassSections',
'type' => \Zend\Form\Element\Hidden::class,
'attributes' => [
'id' => 'glassSections',
],
]);
Any advice would be appreciated!
It's because it was an entity in that field...

Symfony2: Form throws "Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be an array, object given" on submit

I'm using FOSUserBundle in one of my projects.
I've build a form based on the object Employee (that has manytomany with RoleGroup).
Here is the form (part of it):
$builder->add('groups', 'entity', array(
'class' => 'MMAAuthBundle:RoleGroup',
'choices' => $this->groups,
'property' => 'name',
'label' => 'Groups',
'expanded' => true,
'attr' => array("multiple" => true)
));
When I submit the form, I get this error in the Profiler:
at ErrorHandler ->handle ('4096', 'Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be an array, object given, called in /home/mihai/intranet/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 528 and defined', '/home/mihai/intranet/vendor/doctrine/collections/lib/Doctrine/Common/Collections/ArrayCollection.php', '47', array())
in /home/mihai/intranet/vendor/doctrine/collections/lib/Doctrine/Common/Collections/ArrayCollection.php at line 47
How can I make the form return an ArrayCollection, not a RoleGroup object?
I had exactly this problem before, but now I'm stuck here.
Your form is currently not a multiple form and therefore passes a single RoleGroup object instead of an array of RoleGroup objects to the constructor of the Collection.
multiple is a form-option ... and not an HTML-attribute. Therefore ...
$builder->add('groups', 'entity', array(
// This would only render a multiple="true" inside the fields HTML tag
'attr' => array("multiple" => true)
... should be ...
$builder->add('groups', 'entity', array(
// multiple option not wrapped by attribute is correct
"multiple" => true

Add Entity field type to form with event subscriber class

I am doing something very similar to this cookbook example http://symfony.com/doc/current/cookbook/form/dynamic_form_generation.html#adding-an-event-subscriber-to-a-form-class
The main difference is that my field type is an entity and not a text type.
So my field subscriber preSetData method looks like this:
public function preSetData(DataEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
if (null === $data) {
return;
}
if(!$data->getIsCategorized()){
$form->add(
$this->factory->createNamed('categories', 'entity', array(
'class' => 'My\PostBundle\Entity\Category',
'property' => 'name',
'multiple' => true,
'label' => 'Category'
)
)
);
}
}
This is giving the following error
Class does not exist
500 Internal Server Error - ReflectionException
If I add the entity directly in my form type with $builder->add('categories, 'entity', array(... it works fine
Is it possible to attach an entity field type to a form using a field event subscriber in this fashion?
I ran into the same problem, and actually it's because the factory->createNamed() method has more argument than the builder->add
The third argument isn't the options array, but a "data" argument.
So here's what you should do :
$form->add(
$this->factory->createNamed('categories', 'entity', null, array(
'class' => 'My\PostBundle\Entity\Category',
'property' => 'name',
'multiple' => true,
'label' => 'Category'
)
)
);
(add null before the options array)
Whether you attach a field in the type or by means of an event listener/subscriber should make no difference. Either you have a small mistake somewhere (likely), or that's a bug, in which case you should submit it to the issue tracker.

Add a custom dijit element to Zend Dojo Form

I would like to use Dijit elements that are not included into Zend Framework. But I don't know how I should go around it :
$subPrice = new Zend_Dojo_Form_SubForm('priceTab');
$subPrice->setLegend('Tarification :');
$sign = new Zend_Dojo_Form_Decorator_DijitElement('sign');
$sign->setDijitParams(array('dojoType' => 'dijit.form.ToggleButton',
'label' => '-',
'showLabel' => true,
'checked' => true,
'onChange' => 'toggleLabel(val)'
));
$subPrice->addElement($sign);
In my view I just added dojo.require("dijit.form.Button"); in the javascript
However I get this error message from the addElementfunction :
Fatal error: Call to a member function getOrder() on a non-object in D:\www\tuto\library\Zend\Form.php on line 1055
Any help would be extremely appreciated.
Thanks !

Translate select options in Symfony2 class forms

I'm using a class form in Symfony2 Beta3 as follows:
namespace Partners\FrontendBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class ConfigForm extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('no_containers', 'choice', array('choices' => array(1 => 'yes', 0 => 'no')));
...
I want to translate the 'yes' and 'no' options, but I don't know how to use the translator here.
You can use the translation resources as usual. This worked for me:
$builder->add('sex', 'choice', array(
'choices' => array(
1 => 'profile.show.sex.male',
2 => 'profile.show.sex.female',
),
'required' => false,
'label' => 'profile.show.sex.label',
'translation_domain' => 'AcmeUserBundle'
));
And then add your translations to the Resources->translations directory of your Bundle.
Update from #CptSadface:
In symfony 2.7, using the choice_label argument, you can specify the translation domain like this:
'choice_label' => 'typeName',
'choice_translation_domain' => 'messages',
Without specifying the domain, options are not translated.
I searched a while to find an answer, but finally I found out how Symfony translates form content. The easiest way in your case seems to be to just add a translation for "yes" and "no" by adding a YAML or XLIFF translation file to your application (e.g. app/Resources/translations/messages.de.yml) or your bundle. This is described here:
http://symfony.com/doc/current/book/translation.html
The problem - in my opinion - is that you don't seem to be able to use custom translation keys. The guys from FOSUserBundle solve this (or a similar) problem with "Form Themes" (http://symfony.com/doc/2.0/cookbook/form/form_customization.html). Here are two significant lines of code to achieve the usage of the form element id as translation key:
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/Registration/register_content.html.twig#L1 / https://github.com/FriendsOfSymfony/FOSUserBundle/blob/50ab4d8fdfd324c1e722cb982e685abdc111be0b/Resources/views/form.html.twig#L4
By adding a Form Theme you're able to modify pretty much everything of the forms in the templates - this seems to be the right way of doing this.
(Sorry, I had to split two of the links b/c I don't have enough reputation to post more than two links. Sad.)
In symfony 2.7, using the choice_label argument, you can specify the translation domain like this:
'choice_label' => 'typeName',
'choice_translation_domain' => 'messages',
Without specifying the domain, options are not translated.
CptSadface's answer was what helped me with translating my entity choices.
$builder
->add(
'authorizationRoles',
null,
[
'label' => 'app.user.fields.authorization_roles',
'multiple' => true,
'choice_label' => 'name', // entity field storing your translation key
'choice_translation_domain' => 'messages',
]
);