如何将数组(可自定义)添加到 symfony2 表单(使用 sonata Admin)?

How to add an array (customisable) to a symfony2 form (with sonata Admin)?

我有一个 Sonata admin 的简单表格。

我希望用户可以添加一个整数列表(任意多)。然后它将作为数组存储在我的对象中:

[1, 2, 3, 6, 9]

有什么方法可以在不创建另一个 class 来实例化整数的情况下做到这一点?

更新:

我知道如何接近某些东西的唯一方法是使用像这样的选择:

 ->add('type', 'choice', [
                "required"           => true,
                "expanded"           => true,
                "multiple"           => false,
                "choices"            => Campanha::getTypes(),
            ])

但是我的选择有限,我希望用户可以自由添加他想要的数字数量和值

尝试查看 sonata_type_native_collection:

来自the Sonata Admin Docs

This bundle handle the native Symfony collection form type by adding:

  • an add button if you set the allow_add option to true.
  • a delete button if you set the allow_delete option to true.

Symfony collection form type:

This field type is used to render a "collection" of some field or form. In the easiest sense, it could be an array of TextType fields that populate an array emails values.

所以,对于你的情况,可能是这样的:

->add('type', 'sonata_type_native_collection', [
    'required' => true,
    'entry_type' => 'number',
    'options' => [
        // Any options you'd like the integer fields to have.
    ]
])

(当然,这根本没有说明您需要对基础模型进行的更改。)

编辑:根据@Matheus Oliveira 的评论,将 'entry_options' 数组键更改为 'options'

要完成此操作,您只需要 Data Transformer。看一个例子:

namespace AppBundle\Form\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;

class ArrayToStringTransformer implements DataTransformerInterface
{
    public function transform($array)
    {
        if (null === $array) {
            $array = array();
        }

        if (!is_array($array)) {
            throw new TransformationFailedException('Expected an array.');
        }

        return implode(',', $array);
    }

    public function reverseTransform($string)
    {
        if (null === $string || '' === $string) {
            return array();
        }

        if (!is_string($string)) {
            throw new TransformationFailedException('Expected a string.');
        }

        return explode(',', $string);
    }
}

以后在有数组字段的地方使用。为了提高可重用性,让我们 create a custom field type which extends of TextType:

namespace AppBundle\Form\Type;

use AppBundle\Form\DataTransformer\ArrayToStringTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

class ArrayTextType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->addModelTransformer(new ArrayToStringTransformer());
    }

    public function getParent()
    {
        return TextType::class;
    }
}

就是这样!现在您可以使用 ArrayTextType:

安全地管理数组字段
// in any controller context

public function fooAction() 
{
    $data = array(
        'tags' => array(1, 2, 3, 4, 5),
    );

    $form = $this->createFormBuilder($data)
        ->add('tags', ArrayTextType::class)
        ->getForm();

    return $this->render('default/index.html.twig', array('form' => $form->createView()));
}

我们也可以使用任何 Doctrine array 映射字段(即 @ORM\Column(name="tags", type="array"))。

输出结果:


为了更好地输入数据,我建议使用 Bootstrap Tags Input jQuery plugin. See examples here