3

Imagine I have an Article entity.
And in this entity have a report attribute which is an json_array type.

Json_array's data may like
{"key1":"value1","key2":{"k1":"v1","k2","v2"...},"key3":["v1","v2","v3"...]...}.
I mean the json_array may contains simple key:value
or the value may also contains key:vaule
or the value may an array.

Now I don't know how to use symfony form to render and save these json_array like other normal attribute(e.g.,title).
At the same time,I want to manage the key label name with an meaning name just like change the title field's label.
How to achieve this,I feel very difficult.

class Article
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=255)
     */
    private $title;

    /**
     * @var array
     *
     * @ORM\Column(name="report", type="json_array")
     */
    private $report;

}
2
  • Do you want the label to be the value of key1, key2, etc. when editing the form? As in: when the user adds report fields, when editing the article the report fields will have the label the customer filled in him/herself? Commented May 30, 2016 at 11:57
  • The lable just like symfony form's label can customize easily.Don't need the user adds report fields temporarily. Commented May 31, 2016 at 2:18

2 Answers 2

2

Maybe you can use json_decode to pass from json to array and later in the form you can use:

 ->add('someField', null, array('mapped' => false))

And in the success do something with this values

$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// some awesome code here
}

Hope this can help you.

Roger

Sign up to request clarification or add additional context in comments.

1 Comment

You mean add the report's every field(eg,key1,key2,key3...) to the form build and mapped every field false ? If so,how can I transform these not mapped values to the form
2

you can create a data type to manage your report field :

namespace Acme\TestBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

    class ReportType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('key1',TextType::class,array('label' => 'K1'))
                ->add('key2',TextType::class,array('label' => 'K2'))
            ;
        }

        public function getName()
        {
            return 'report';
        }
    }

Then declare the new data type :

# src/Acme/TestBundle/Resources/config/services.yml
services:
    acme_test.form.type.report:
        class: Acme\TestBundle\Form\Type\ReportType
        tags:
            - { name: form.type, alias: report }

And finally use this new dataType in your form :

->add('reports',
            'collection',
            array(
                'type'=>'report',
                'prototype'=>true,
                'allow_add'=>true,
                'allow_delete'=>true,
                'options'=>array(
                )
            )
        )

8 Comments

If in this way ,I can't manage the label ,for example I want to change the key1 display k1,and key2 display k2 and so on .At the same time ,every field type may not the same .some are text types and some are datetime type.
Thanks for your reply,I am sorry I foget to say I use symfony 2.8. And in symfony2.8 there's seems no type option in collection .In 2.8 is entry_type .I guess you reply version is less than 2.8 and I am so excited seeing your answer.
I found no success. The label and the field type both not working( Label is still the original and the field type still the same).
And I found define the report service seems not necessary.
I updated my answer for sf 2.8, cant test as I'm not home :( , you need to adjust the types for keys to suit your needs
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.