I added 2 entities to my project ( BookAuthor, BookSeries ) with php bin/console make:entity
command, Then I generated a form class with make:form
command.
While testing I added a few Authors but when I tried opening the add/Series
route I get this error message:
Object of class App\Entity\BookAuthor could not be converted to string
Relevant code from Form Builder class:
class BookSeriesType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('description')
->add('SeriesAuthor', )
->add('Submit', SubmitType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => BookSeries::class,
]);
}
}
SeriesAuthor
there is supposed to fetch the list of available Authors from the database.
BookSeries has a OneToMany relation with BookAuthor Entity. While the BookAuthor table was empty the page loaded fine after adding authors the page started giving the error
Relevant code from the Controller.php file:
public function addBookSeries(Request $request)
{
$new_series = new BookSeries();
$form = $this->createForm(BookSeriesType::class, $new_series);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$em = $this->getDoctrine()->getManager();
$em->persist($new_series);
$em->flush();
return $this->redirect($this->generateUrl('post.manage'));
}
return $this->render("post/create.html.twig",[
'form' => $form->createView()
]);
StackTrace from the error:
Error:
Object of class App\Entity\BookAuthor could not be converted to string
at C:\xampp\htdocs\project-php-7.3-dev\vendor\symfony\doctrine-bridge\Form\Type\DoctrineType.php:58
at Symfony\Bridge\Doctrine\Form\Type\DoctrineType::createChoiceLabel(object(BookAuthor), 0, '2')
(C:\xampp\htdocs\project-php-7.3-dev\vendor\symfony\form\ChoiceList\Factory\DefaultChoiceListFactory.php:179)
at Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory::addChoiceView(object(BookAuthor), '2', array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceLabel'), array(0), array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceName'), null, null, array(), array(), array())
(C:\xampp\htdocs\project-php-7.3-dev\vendor\symfony\form\ChoiceList\Factory\DefaultChoiceListFactory.php:248)
at Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory::addChoiceViewsFromStructuredValues(array('2'), array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceLabel'), array(object(BookAuthor)), array(0), array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceName'), null, null, array(), array(), array())
(C:\xampp\htdocs\project-php-7.3-dev\vendor\symfony\form\ChoiceList\Factory\DefaultChoiceListFactory.php:143)
at Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory->createView(object(LazyChoiceList), null, array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceLabel'), array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceName'), null, null)
(C:\xampp\htdocs\project-php-7.3-dev\vendor\symfony\form\ChoiceList\Factory\PropertyAccessDecorator.php:220)
at Symfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator->createView(object(LazyChoiceList), array(), array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceLabel'), array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceName'), null, null)
(C:\xampp\htdocs\project-php-7.3-dev\vendor\symfony\form\ChoiceList\Factory\CachingFactoryDecorator.php:225)
at Symfony\Component\Form\ChoiceList\Factory\CachingFactoryDecorator->createView(object(LazyChoiceList), array(), array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceLabel'), array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceName'), null, null)
(C:\xampp\htdocs\project-php-7.3-dev\vendor\symfony\form\Extension\Core\Type\ChoiceType.php:437)
at Symfony\Component\Form\Extension\Core\Type\ChoiceType->createChoiceListView(object(LazyChoiceList), array('block_name' => null, 'disabled' => false, 'label' => null, 'label_format' => null, 'label_translation_parameters' => array(), 'attr_translation_parameters' => array(), 'translation_domain' => null, 'auto_initialize' => true, 'trim' => false, 'property_path' => null, 'mapped' => true, 'by_reference' => true, 'inherit_data' => false, 'method' => 'POST', 'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.', 'allow_file_upload' => false, 'help_translation_parameters' => array(), 'error_mapping' => array(), 'invalid_message' => 'This value is not valid.', 'invalid_message_parameters' => array(), 'allow_extra_fields' => false, 'extra_fields_message' => 'This form should not contain extra fields.', 'csrf_protection' => true, 'csrf_field_name' => '_token', 'csrf_message' => 'The CSRF token is invalid. Please try to resubmit the form.', 'csrf_token_manager' => object(CsrfTokenManager), 'csrf_token_id' => null, 'expanded' => false, 'block_prefix' => null, 'row_attr' => array(), 'label_html' => false, 'attr' => array(), 'data_class' => null, 'multiple' => false, 'empty_data' => '', 'required' => true, 'error_bubbling' => false, 'label_attr' => array(), 'compound' => false, 'action' => '', 'upload_max_size_message' => object(Closure), 'help' => null, 'help_attr' => array(), 'help_html' => false, 'is_empty_callback' => null, 'validation_groups' => null, 'constraints' => array(), 'choices' => null, 'choice_filter' => null, 'em' => object(EntityManager), 'class' => 'App\\Entity\\BookAuthor', 'query_builder' => null, 'id_reader' => object(IdReader), 'choice_loader' => object(ChoiceLoader), 'choice_label' => object(ChoiceLabel), 'choice_name' => object(ChoiceFieldName), 'choice_value' => object(ChoiceValue), 'choice_attr' => null, 'preferred_choices' => array(), 'group_by' => null, 'placeholder' => null, 'choice_translation_domain' => false))
(C:\xampp\htdocs\project-php-7.3-dev\vendor\symfony\form\Extension\Core\Type\ChoiceType.php:205)
at Symfony\Component\Form\Extension\Core\Type\ChoiceType->buildView(object(FormView), object(Form), array('block_name' => null, 'disabled' => false, 'label' => null, 'label_format' => null, 'label_translation_parameters' => array(), 'attr_translation_parameters' => array(), 'translation_domain' => null, 'auto_initialize' => true, 'trim' => false, 'property_path' => null, 'mapped' => true, 'by_reference' => true, 'inherit_data' => false, 'method' => 'POST', 'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.', 'allow_file_upload' => false, 'help_translation_parameters' => array(), 'error_mapping' => array(), 'invalid_message' => 'This value is not valid.', 'invalid_message_parameters' => array(), 'allow_extra_fields' => false, 'extra_fields_message' => 'This form should not contain extra fields.', 'csrf_protection' => true, 'csrf_field_name' => '_token', 'csrf_message' => 'The CSRF token is invalid. Please try to resubmit the form.', 'csrf_token_manager' => object(CsrfTokenManager), 'csrf_token_id' => null, 'expanded' => false, 'block_prefix' => null, 'row_attr' => array(), 'label_html' => false, 'attr' => array(), 'data_class' => null, 'multiple' => false, 'empty_data' => '', 'required' => true, 'error_bubbling' => false, 'label_attr' => array(), 'compound' => false, 'action' => '', 'upload_max_size_message' => object(Closure), 'help' => null, 'help_attr' => array(), 'help_html' => false, 'is_empty_callback' => null, 'validation_groups' => null, 'constraints' => array(), 'choices' => null, 'choice_filter' => null, 'em' => object(EntityManager), 'class' => 'App\\Entity\\BookAuthor', 'query_builder' => null, 'id_reader' => object(IdReader), 'choice_loader' => object(ChoiceLoader), 'choice_label' => object(ChoiceLabel), 'choice_name' => object(ChoiceFieldName), 'choice_value' => object(ChoiceValue), 'choice_attr' => null, 'preferred_choices' => array(), 'group_by' => null, 'placeholder' => null, 'choice_translation_domain' => false))
(C:\xampp\htdocs\project-php-7.3-dev\vendor\symfony\form\ResolvedFormType.php:146)
at Symfony\Component\Form\ResolvedFormType->buildView(object(FormView), object(Form), array('block_name' => null, 'disabled' => false, 'label' => null, 'label_format' => null, 'label_translation_parameters' => array(), 'attr_translation_parameters' => array(), 'translation_domain' => null, 'auto_initialize' => true, 'trim' => false, 'property_path' => null, 'mapped' => true, 'by_reference' => true, 'inherit_data' => false, 'method' => 'POST', 'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.', 'allow_file_upload' => false, 'help_translation_parameters' => array(), 'error_mapping' => array(), 'invalid_message' => 'This value is not valid.', 'invalid_message_parameters' => array(), 'allow_extra_fields' => false, 'extra_fields_message' => 'This form should not contain extra fields.', 'csrf_protection' => true, 'csrf_field_name' => '_token', 'csrf_message' => 'The CSRF token is invalid. Please try to resubmit the form.', 'csrf_token_manager' => object(CsrfTokenManager), 'csrf_token_id' => null, 'expanded' => false, 'block_prefix' => null, 'row_attr' => array(), 'label_html' => false, 'attr' => array(), 'data_class' => null, 'multiple' => false, 'empty_data' => '', 'required' => true, 'error_bubbling' => false, 'label_attr' => array(), 'compound' => false, 'action' => '', 'upload_max_size_message' => object(Closure), 'help' => null, 'help_attr' => array(), 'help_html' => false, 'is_empty_callback' => null, 'validation_groups' => null, 'constraints' => array(), 'choices' => null, 'choice_filter' => null, 'em' => object(EntityManager), 'class' => 'App\\Entity\\BookAuthor', 'query_builder' => null, 'id_reader' => object(IdReader), 'choice_loader' => object(ChoiceLoader), 'choice_label' => object(ChoiceLabel), 'choice_name' => object(ChoiceFieldName), 'choice_value' => object(ChoiceValue), 'choice_attr' => null, 'preferred_choices' => array(), 'group_by' => null, 'placeholder' => null, 'choice_translation_domain' => false))
(C:\xampp\htdocs\project-php-7.3-dev\vendor\symfony\form\Extension\DataCollector\Proxy\ResolvedTypeDataCollectorProxy.php:103)
at Symfony\Component\Form\Extension\DataCollector\Proxy\ResolvedTypeDataCollectorProxy->buildView(object(FormView), object(Form), array('block_name' => null, 'disabled' => false, 'label' => null, 'label_format' => null, 'label_translation_parameters' => array(), 'attr_translation_parameters' => array(), 'translation_domain' => null, 'auto_initialize' => true, 'trim' => false, 'property_path' => null, 'mapped' => true, 'by_reference' => true, 'inherit_data' => false, 'method' => 'POST', 'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.', 'allow_file_upload' => false, 'help_translation_parameters' => array(), 'error_mapping' => array(), 'invalid_message' => 'This value is not valid.', 'invalid_message_parameters' => array(), 'allow_extra_fields' => false, 'extra_fields_message' => 'This form should not contain extra fields.', 'csrf_protection' => true, 'csrf_field_name' => '_token', 'csrf_message' => 'The CSRF token is invalid. Please try to resubmit the form.', 'csrf_token_manager' => object(CsrfTokenManager), 'csrf_token_id' => null, 'expanded' => false, 'block_prefix' => null, 'row_attr' => array(), 'label_html' => false, 'attr' => array(), 'data_class' => null, 'multiple' => false, 'empty_data' => '', 'required' => true, 'error_bubbling' => false, 'label_attr' => array(), 'compound' => false, 'action' => '', 'upload_max_size_message' => object(Closure), 'help' => null, 'help_attr' => array(), 'help_html' => false, 'is_empty_callback' => null, 'validation_groups' => null, 'constraints' => array(), 'choices' => null, 'choice_filter' => null, 'em' => object(EntityManager), 'class' => 'App\\Entity\\BookAuthor', 'query_builder' => null, 'id_reader' => object(IdReader), 'choice_loader' => object(ChoiceLoader), 'choice_label' => object(ChoiceLabel), 'choice_name' => object(ChoiceFieldName), 'choice_value' => object(ChoiceValue), 'choice_attr' => null, 'preferred_choices' => array(), 'group_by' => null, 'placeholder' => null, 'choice_translation_domain' => false))
(C:\xampp\htdocs\project-php-7.3-dev\vendor\symfony\form\ResolvedFormType.php:143)
at Symfony\Component\Form\ResolvedFormType->buildView(object(FormView), object(Form), array('block_name' => null, 'disabled' => false, 'label' => null, 'label_format' => null, 'label_translation_parameters' => array(), 'attr_translation_parameters' => array(), 'translation_domain' => null, 'auto_initialize' => true, 'trim' => false, 'property_path' => null, 'mapped' => true, 'by_reference' => true, 'inherit_data' => false, 'method' => 'POST', 'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.', 'allow_file_upload' => false, 'help_translation_parameters' => array(), 'error_mapping' => array(), 'invalid_message' => 'This value is not valid.', 'invalid_message_parameters' => array(), 'allow_extra_fields' => false, 'extra_fields_message' => 'This form should not contain extra fields.', 'csrf_protection' => true, 'csrf_field_name' => '_token', 'csrf_message' => 'The CSRF token is invalid. Please try to resubmit the form.', 'csrf_token_manager' => object(CsrfTokenManager), 'csrf_token_id' => null, 'expanded' => false, 'block_prefix' => null, 'row_attr' => array(), 'label_html' => false, 'attr' => array(), 'data_class' => null, 'multiple' => false, 'empty_data' => '', 'required' => true, 'error_bubbling' => false, 'label_attr' => array(), 'compound' => false, 'action' => '', 'upload_max_size_message' => object(Closure), 'help' => null, 'help_attr' => array(), 'help_html' => false, 'is_empty_callback' => null, 'validation_groups' => null, 'constraints' => array(), 'choices' => null, 'choice_filter' => null, 'em' => object(EntityManager), 'class' => 'App\\Entity\\BookAuthor', 'query_builder' => null, 'id_reader' => object(IdReader), 'choice_loader' => object(ChoiceLoader), 'choice_label' => object(ChoiceLabel), 'choice_name' => object(ChoiceFieldName), 'choice_value' => object(ChoiceValue), 'choice_attr' => null, 'preferred_choices' => array(), 'group_by' => null, 'placeholder' => null, 'choice_translation_domain' => false))
(C:\xampp\htdocs\project-php-7.3-dev\vendor\symfony\form\Extension\DataCollector\Proxy\ResolvedTypeDataCollectorProxy.php:103)
at Symfony\Component\Form\Extension\DataCollector\Proxy\ResolvedTypeDataCollectorProxy->buildView(object(FormView), object(Form), array('block_name' => null, 'disabled' => false, 'label' => null, 'label_format' => null, 'label_translation_parameters' => array(), 'attr_translation_parameters' => array(), 'translation_domain' => null, 'auto_initialize' => true, 'trim' => false, 'property_path' => null, 'mapped' => true, 'by_reference' => true, 'inherit_data' => false, 'method' => 'POST', 'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.', 'allow_file_upload' => false, 'help_translation_parameters' => array(), 'error_mapping' => array(), 'invalid_message' => 'This value is not valid.', 'invalid_message_parameters' => array(), 'allow_extra_fields' => false, 'extra_fields_message' => 'This form should not contain extra fields.', 'csrf_protection' => true, 'csrf_field_name' => '_token', 'csrf_message' => 'The CSRF token is invalid. Please try to resubmit the form.', 'csrf_token_manager' => object(CsrfTokenManager), 'csrf_token_id' => null, 'expanded' => false, 'block_prefix' => null, 'row_attr' => array(), 'label_html' => false, 'attr' => array(), 'data_class' => null, 'multiple' => false, 'empty_data' => '', 'required' => true, 'error_bubbling' => false, 'label_attr' => array(), 'compound' => false, 'action' => '', 'upload_max_size_message' => object(Closure), 'help' => null, 'help_attr' => array(), 'help_html' => false, 'is_empty_callback' => null, 'validation_groups' => null, 'constraints' => array(), 'choices' => null, 'choice_filter' => null, 'em' => object(EntityManager), 'class' => 'App\\Entity\\BookAuthor', 'query_builder' => null, 'id_reader' => object(IdReader), 'choice_loader' => object(ChoiceLoader), 'choice_label' => object(ChoiceLabel), 'choice_name' => object(ChoiceFieldName), 'choice_value' => object(ChoiceValue), 'choice_attr' => null, 'preferred_choices' => array(), 'group_by' => null, 'placeholder' => null, 'choice_translation_domain' => false))
(C:\xampp\htdocs\project-php-7.3-dev\vendor\symfony\form\Form.php:1041)
at Symfony\Component\Form\Form->createView(object(FormView))
(C:\xampp\htdocs\project-php-7.3-dev\vendor\symfony\form\Form.php:1044)
at Symfony\Component\Form\Form->createView()
(C:\xampp\htdocs\project-php-7.3-dev\src\Controller\BookDataController.php:59)
at App\Controller\BookDataController->addBookSeries(object(Request))
(C:\xampp\htdocs\project-php-7.3-dev\vendor\symfony\http-kernel\HttpKernel.php:157)
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
(C:\xampp\htdocs\project-php-7.3-dev\vendor\symfony\http-kernel\HttpKernel.php:79)
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
(C:\xampp\htdocs\project-php-7.3-dev\vendor\symfony\http-kernel\Kernel.php:196)
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
(C:\xampp\htdocs\project-php-7.3-dev\public\index.php:28)
This tutorial on SymfonyCasts helped me solve it.
Apparently I had to configure the form builder a bit.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('seriesAuthor', EntityType::class, [
'class' => BookAuthor::class,
'choice_label' => function(BookAuthor $author) {
return sprintf('%s %s', $author->getFirstname(), $author->getLastname());
},
'placeholder' => 'Choose an author'
])
->add('description')
->add('Submit', SubmitType::class)
;
}