Symfony2 An exception has been thrown during the rendering of a template strtr() expects parameter 1 to be string, object given
Although not the most convenient way to achieve the desired results, I ended up constructing a form inside the Controller action and when it came to addressing the date field, I used it like so:
$surveys = $item_repo->findAll();
foreach($surveys as $survey){
array_push($dates, $survey->getSurveyDate()->format('d/m/Y') );
}
$question = array();
$form = $this->createFormBuilder( $question )
->add('survey', 'choice', array(
'choices' => $dates ))
->add('section', 'text', array('required' => true, 'trim' => true))
->add('sub_section', 'text', array('required' => true, 'trim' => true))
->add('description', 'text', array('required' => true, 'trim' => true))
->add('points', 'integer', array('required' => true, 'trim' => true))
->getForm();
In specifying the formatting of the Datetime object, like so (->format('d/m/Y') ), TWIG was able to process the data without any errors.
Comments
-
Matt G almost 2 years
I am trying to get a Date type field from the entity and I encounter the following error:
An exception has been thrown during the rendering of a template ("Warning: strtr()
expects parameter 1 to be string, object given in /var/www/feedyourmind_symfony/vendor /symfony/src/Symfony/Component/Translation/IdentityTranslator.php line 62") in form_div_layout.html.twig at line 37.I am fairly new to Symfony2 and can not seem to figure out why I would be getting this error. Perhaps there is something wrong with my Entities? I do not know and I would really like some assistance if possible.
Although the error points to an issue with the rendering of a template, I feel that the real error lies with the Entity and the date field not correctly functioning.
Here is the basic code in my Controller:
public function addQuestionAction(Request $request){ $question = new Question(); $form = $this->createForm(new QuestionType(), $question); return $this->render('LaPorchettaWebBundle:Default:add_question.html.twig', array( 'form' => $form->createView(), )); }
Here is the TWIG view:
{% extends "LaPorchettaWebBundle:Default:test.html.twig" %} {% block pageTitle %}LaPorchetta Create A Question{% endblock %} {% block content %} <div id="outer"> <form name="petEntry" action="" method="post" enctype="multipart/form-data" > {{ form_errors(form) }} <div class="left"> {{ form_label(form.survey) }} </div> <div class="right"> {{ form_widget(form.survey) }} {{ form_errors(form.survey) }} </div> <div class="left"> {{ form_label(form.section) }} </div> <div class="right"> {{ form_widget(form.section) }} {{ form_errors(form.section) }} </div> <div class="left"> {{ form_label(form.sub_section) }} </div> <div class="right"> {{ form_widget(form.sub_section) }} {{ form_errors(form.sub_section) }} </div> <div class="left"> {{ form_label(form.description) }} </div> <div class="right"> {{ form_widget(form.description) }} {{ form_errors(form.description) }} </div> <div class="left"> {{ form_label(form.points) }} </div> <div class="right"> {{ form_widget(form.points) }} {{ form_errors(form.points) }} </div> <div id="inputs"> <input type="button" id="btnCancel" name="cancel" value="Cancel" onclick="window.location = '' " /> <input id="update" type="submit" value="submit" /> </div> </div> </form> </div> {% endblock %}
I have the following entities:
<?php namespace LaPorchetta\WebBundle\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * @ORM\Entity(repositoryClass="LaPorchetta\WebBundle\Repository\SurveyRepository") * @ORM\HasLifecycleCallbacks() * @ORM\Table(name="Surveys") */ class Survey { public function __construct() { $this->question = new ArrayCollection(); $this->store = new ArrayCollection(); } /** * @ORM\Id @ORM\Column(type="integer") * @ORM\GeneratedValue */ protected $id; /** * @ORM\Column(type="date") */ protected $survey_date; /** * @ORM\OneToMany(targetEntity="Question", mappedBy="survey") */ protected $question = null; /** * @ORM\OneToOne(targetEntity="Store", inversedBy="survey") */ protected $store = null; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * @ORM\prePersist */ public function setSurveyDate() { $this->survey_date = new \DateTime(); } /** * Get survey_date * * @return date */ public function getSurveyDate() { return $this->survey_date; } /** * Add question * * @param LaPorchetta\WebBundle\Entity\Question $question */ public function addQuestion(\LaPorchetta\WebBundle\Entity\Question $question) { $this->question[] = $question; } /** * Get question * * @return Doctrine\Common\Collections\Collection */ public function getQuestion() { return $this->question; } /** * Set store * * @param LaPorchetta\WebBundle\Entity\Store $store */ public function setStore(\LaPorchetta\WebBundle\Entity\Store $store) { $this->store = $store; } /** * Get store * * @return LaPorchetta\WebBundle\Entity\Store */ public function getStore() { return $this->store; } /** * Get action_item * * @return Doctrine\Common\Collections\Collection */ public function getActionItem() { return $this->action_item; } /** * Set action_item * * @param LaPorchetta\WebBundle\Entity\Question $actionItem */ public function setActionItem(\LaPorchetta\WebBundle\Entity\Question $actionItem) { $this->action_item = $actionItem; } }
Entity Type -> Questions
<?php namespace LaPorchetta\WebBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * @ORM\Entity(repositoryClass="LaPorchetta\WebBundle\Repository\QuestionRepository") * @ORM\Table(name="Questions") */ class Question { /** * @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue */ protected $id; /** * @ORM\ManyToOne(targetEntity="Survey", inversedBy="question") */ protected $survey; /** * @ORM\Column(type="string") */ protected $section; /** * @ORM\Column(type="string") */ protected $sub_section; /** * @ORM\Column(type="string") */ protected $description; /** * @ORM\Column(type="integer") */ protected $points; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set section * * @param string $section */ public function setSection($section) { $this->section = $section; } /** * Get section * * @return string */ public function getSection() { return $this->section; } /** * Set sub_section * * @param string $subSection */ public function setSubSection($subSection) { $this->sub_section = $subSection; } /** * Get sub_section * * @return string */ public function getSubSection() { return $this->sub_section; } /** * Set description * * @param string $description */ public function setDescription($description) { $this->description = $description; } /** * Get description * * @return string */ public function getDescription() { return $this->description; } /** * Set survey * * @param LaPorchetta\WebBundle\Entity\Survey $survey */ public function setSurvey(\LaPorchetta\WebBundle\Entity\Survey $survey) { $this->survey = $survey; } /** * Get survey * * @return LaPorchetta\WebBundle\Entity\Survey */ public function getSurvey() { return $this->survey; } /** * Set points * * @param integer $points */ public function setPoints($points) { $this->points = $points; } /** * Get points * * @return integer */ public function getPoints() { return $this->points; } public function __construct() { $this->action_item = new \Doctrine\Common\Collections\ArrayCollection(); } /** * Add action_item * * @param LaPorchetta\WebBundle\Entity\Survey $actionItem */ public function addSurvey(\LaPorchetta\WebBundle\Entity\Survey $actionItem) { $this->action_item[] = $actionItem; } /** * Get action_item * * @return Doctrine\Common\Collections\Collection */ public function getActionItem() { return $this->action_item; } /** * Set action_item * * @param LaPorchetta\WebBundle\Entity\Survey $actionItem */ public function setActionItem(\LaPorchetta\WebBundle\Entity\Survey $actionItem) { $this->action_item = $actionItem; } }
I have the following QuestionType:
<?php namespace LaPorchetta\WebBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilder; use Doctrine\ORM\EntityRepository; class QuestionType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder ->add('survey', 'entity', array( 'class'=>'LaPorchettaWebBundle:Survey', 'property'=>'survey_date', 'multiple' => true, 'required' => true, 'query_builder' => function(EntityRepository $er) { return $er->createQueryBuilder('s')->orderBy('s.survey_date', 'ASC'); })) ->add('section', 'text') ->add('sub_section', 'text') ->add('description', 'text') ->add('points', 'integer'); } public function getName() { return 'LaPorchetta_WebBundle_QuestionType'; } }