php - Symfony datatransfomers json to array for choice type
Get the solution ↓↓↓I have a mapped fields 'days' in my entity that contains some json:
{"monday":1,"tuesday":1,"wednesday":1,"thursday":1,"friday":1,"saturday":0,"sunday":0}
And i want to render it in some checkboxes in my form.
So i mapped this fields in my form, and add some datatransformers to transform this json to array.
But nothing render. I did not find anything about this specific case: datatransformers mapped field to array for choice type.
Here is my action in controler:
/**
public function indexAction(Request $request, ScolariteManager $scolariteManager)
{
$scolarite = $scolariteManager->find(2276);
$form = $this->createForm(Toto::class, $scolarite);
$form->handleRequest($request);
return $this->render('AciaProAppBundle:Default:index.html.twig', [
'form' => $form->createView(),
]);
}
Here is my form:
<?php
namespace AciaPro\AppBundle\Form\Type;
use AciaPro\AppBundle\Entity\Scolarite;
use AciaPro\AppBundle\Form\DataTransformer\TransformToto;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class Toto extends AbstractType
{
/**
* @var TransformToto
*/
private $transformToto;
public function __construct(TransformToto $transformToto)
{
$this->transformToto = $transformToto;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('joursCantineArray', ChoiceType::class, [
'expanded' => true,
'multiple' => true,
])->addModelTransformer($this->transformToto)
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Scolarite::class,
]);
}
}
Here is my datatransformers:
<?php
namespace AciaPro\AppBundle\Form\DataTransformer;
use AciaPro\AppBundle\Entity\Scolarite;
use Symfony\Component\Form\DataTransformerInterface;
class TransformToto implements DataTransformerInterface
{
/**
* {@inheritdoc}
*/
public function transform($data)
{
/** @var Scolarite $data */
$data->setJoursCantineArray(json_decode($data->getJoursCantine(), true));
return $data;
}
/**
* {@inheritdoc}
*/
public function reverseTransform($data)
{
/** @var Scolarite $data */
$data->setJoursCantine(json_encode($data->getJoursCantineArray()));
return $data;
}
}
And here is my entity
<?php
namespace AciaPro\AppBundle\Entity;
/**
* Scolarite
*/
class Scolarite
{
/**
* @var integer
*/
private $id_scolarite;
/**
* @var string
*/
private $jours_cantine;
/**
* @var array
*/
private $jours_cantine_array;
/**
* @return int
*/
public function getIdScolarite()
{
return $this->id_scolarite;
}
/**
* @param int $id_scolarite
* @return Scolarite
*/
public function setIdScolarite(int $id_scolarite)
{
$this->id_scolarite = $id_scolarite;
return $this;
}
/**
* @return string
*/
public function getJoursCantine()
{
return $this->jours_cantine;
}
/**
* @param string $jours_cantine
* @return Scolarite
*/
public function setJoursCantine(string $jours_cantine)
{
$this->jours_cantine = $jours_cantine;
return $this;
}
/**
* @return array
*/
public function getJoursCantineArray()
{
return $this->jours_cantine_array;
}
/**
* @param array $jours_cantine_array
* @return Scolarite
*/
public function setJoursCantineArray(array $jours_cantine_array)
{
$this->jours_cantine_array = $jours_cantine_array;
return $this;
}
}
And the twig:
<html>
<head>
</head>
<body>
{{ form_start(form) }}
{{ form_row(form.joursCantineArray) }}
{{ form_end(form) }}
</body>
</html>
The resutl display the label and nothing else
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: illegal string offset
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.