ManyToMany nullable not allowed - symfony

I have a scenario like this:
tracks ManyToMany instruments
I create the form in this way:
$builder
->add('instruments', 'entity_hidden', array(
'attr' => array('class' => 'one_req'),
'label' => 'form.instrument',
'translation_domain' => 'label',
'required' => false,
'class' => 'Acme\DemoBundle\Entity\Instrument'
))
"hidden_entity" is given by a custom transformer that you can find here: gist
although I believe that is not the problem.
The problem is that the user may not even choose any instrument (such asamong other specified with "required" => false).
When I go to save my returns this error:
Neither the property 'instruments' nor one of the methods 'setInstruments()',
'__set()' or '__call()' exist and have public access in
class 'Acme\DemoBundle\Entity\SoundtrackVersion'.
I do not understand why ..
Where am I doing wrong?

It sounds very obvious, but error can not lie. Check that Acme\DemoBundle\Entity\SoundtrackVersion entity has property instruments and methods setInstruments() and getInstruments().

Related

Symfony2 Sonata Admin IvoryCKEditor wrong render for some reason

I am trying to implement IvoryCKEditor Bundle to my SonataAdmin entities and I am witnessing some very strange errors/bugs/mistakes... I dont even know..
So when I want to render a simple textarea field and add some longtext to it I simply do something like this:
/**
* #param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
// ->add('id', 'hidden')
->add('name')
->add('contentEn', 'ckeditor', array(
'attr' => array('cols' => '8', 'rows' => '8')))
->add('contentEs', 'ckeditor', array(
'attr' => array('cols' => '8', 'rows' => '8')))
->add('status')
;
}
This works like a charm.. However if I have mapped entities and I want to show its fields I use sonata collection:
->add('translations', 'sonata_type_collection',
array(
'required' => false,
'label' => false,
),
array(
'edit' => 'inline',
'inline' => 'standard',
)
)
And in the mapped entities admin I do this again:
$formMapper
->with('Item Info')
//->add('id')
->add('product_name')
->add('description_for_lbi', 'ckeditor')
->add('short_description', 'ckeditor')
->add('long_description', 'ckeditor')
->add('conditions', 'ckeditor')
->add('language', null, array('required' => true))
->end()
;
Now here is the problem. Its seems the ckeditor form the collection is rendering in a completely different way.
The first example renders an iframe and makes the ckeditor look "clean".
In the collection ckeditor is rendering in a completely different way, no iframes.. And for the editor to show up I have to click on the field first.. And the field has no borders... I really dont know how to explain this.
So I guess my question is, why the ckeditor is rendering completely different when I am using it in a collection. Am I doing something wrong?
If you guys dont understand what I mean I can post some screens...
UPDATE
I thing the problem is here:
'edit' => 'inline',
'inline' => 'standard',
this makes the editor look bad. However if I remove these lines I get error:
Error: Maximum function nesting level of '100' reached, aborting!
This error is when I am trying to edit an object
You are completely right with your edit. inline editing in ckeditor breaks if you have 2 or more instances of the ckeditor config.
My suggestion is to drop inline editing.
For the second part, the error comes from xdebug, which cannot follow your object nesting level because it exceeds this limit.
In order to fix this (which is recommended, as limit = 100 is way too low for symfony2), please see this
article.

Save form data temporary

The user has the ability to filter a list of data. For this use case i build a form type "UserFilterType", which look like this.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('firstName', 'text', array('attr' => array('maxlength' => 255)))
->add('roles', 'entity', array(
'class' => 'ApplicationUserBundle:Role',
'property' => 'name',
'label' => false,
'multiple' => false,
'expanded' => false,
'empty_value' => 'msg.role.all',
'translation_domain' => 'role', 'required' => false)
));
}
So far everything works very well. The user can filter the output. But if the user wants to sort the output, eg by first name. A get request executed and filter options lost. I will save the filter options temporary, maybe in a session.
I try the following option:
$session = $this->getRequest()->getSession();
$session->set('userFilter', serialize($form->getData());
.....
$form->setData(unserialize($session->set('userFilter')));
The form fields are filled out correctly, but if i execute the filter again with a post request i get the following error message
"Entities passed to the choice field must be managed. Maybe persist them in the entity manager?"
Does anyone have any idea how I should proceed? I hope I could describe my problem understandable.
Instead of field type 'entity', you might want to use 'choice' and set the querybuilder for the choicelist in the form.
This is caused by the fact, that 'entity' type field expects an Entity as a value. When you unserialize the data, even if it was an Entity from the database, the EntityManager doesn't know this(the unserialized object is just an instance of ApplicationUserBundle:Role, it might not be in the database yet) so this is not a "managed" entity.
You can also add the EntityManager that you found a "lost lamb" and add it back to the manager by:
$filters = unserialize($session->set('userFilter'));
$roles = $filters['roles'];
$entityManager->merge($roles);
This way you tell the EntityManager that this object is already persisted(through ->persist) and EntityManager should treat it as an object fetched from the database
but you need to test it

symfony2 FromBuilder multiple checkboxes from entity instead of using "choice"

How can I get multiple Cehckboxes instead a choice field in Symfony? Actually, I'm using:
->add('usergroups', 'entity', array('class' => 'PrUserBundle:Group','property' => 'name','required' => true))
This will output a select-Field..
In this case, it would be better to output checkboxes as it is easier to handle for the user.
http://symfony.com/doc/current/reference/forms/types/checkbox.html at, there is nothing helpful about multiple select....
Do I have to build an array and place it by myself?
Use the expanded option for choice and entity fields.
->add('usergroups', 'entity', array('class' => 'PrUserBundle:Group','property' => 'name','required' => true, 'expanded' => true,))

How to validate two instances of the same entity?

Use case
I am learning Symfony2 and am creating a Table Tennis tracking app to learn the framework. I have configured my entities as follows.
Player 1..n Result n..1 Match
On my form I'd like to validate that the scores for a match are correct.
Implementation
Match has an ArrayCollection() of results.
My MatchType and ResultType forms contain the following.
// Form\MatchType
$builder->add('matchType', 'entity', array(
'class' => 'PingPongMatchesBundle:MatchType',
'property' => 'name',
)
)
->add('results', 'collection', array(
'type' => new ResultType(),
'allow_add' => true,
'by_reference' => false,
)
)
->add('notes');
// Form\ResultType
$builder->add('player', 'entity', array(
'class' => 'PingPongPlayerBundle:Player',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('p')
->orderBy('p.firstName', 'ASC');
},
))
->add('score');
Issue
I need to be able to validate the scores. However I'm not sure how to approach this type of validation as I need to compare two instances of my Result#score in order to know if they are valid or not.
Is anyone able to suggest a method or approach that I could use in order to be able to compare Result#score between the two different instances? Can I validate the ArrayCollection in the Match entity for example?
You could creat a custom validator constraint on Match entity.
http://symfony.com/doc/2.0/cookbook/validation/custom_constraint.html
Take a look in Callback constraint:
http://symfony.com/doc/2.1/reference/constraints/Callback.html

How to order related propel model in symfony2 form

I am using symfony2 form along with propel. I am having trouble on how to order the related entity from propel's model.
The abstracted code below, is taken from http://www.propelorm.org/cookbook/symfony2/mastering-symfony2-forms-with-propel.html, which show how to relate them together. I am wondering how do I order them.
$builder->add('author', 'model', array(
'class' => 'Acme\LibraryBundle\Model\Author',
));
For Example, If in my author table there are columns name, age,.... How should make it to order by name.
$builder->add('author', 'model', array(
'class' => 'Acme\LibraryBundle\Model\Author',
'query' => \Acme\LibraryBundle\Mode\AuthorQuery::create()->orderByName()
));

Resources