1

好吧,老实说吧。不喜欢 symfony 决定对他们的核心文件做出的一些决定,所以我试图覆盖它们。例如

Symfony/Component/Form/Extension/Core/Type/FieldType.php

如果 FormView 有父级,我正在尝试更改呈现的名称,因为它们会进行一些很酷的字符串格式化......

我只是想做到这一点,$fullName并且$id两者兼而有之$form->getName()

 public function buildView(FormView $view, FormInterface $form)
    {
        $name = $form->getName();

        if ($view->hasParent()) {
            $parentId = $view->getParent()->get('id');
            $parentFullName = $view->getParent()->get('full_name');

            // Custom Logic
            //$id = sprintf('%s_%s', $parentId, $name);
            //$fullName = sprintf('%s[%s]', $parentFullName, $name);
            $id = $form->getName();
            $fullName = $form->getName();
        } else {
            $id = $name;
            $fullName = $name;
        }

        $types = array();
        foreach ($form->getTypes() as $type) {
            $types[] = $type->getName();
        }

        $view
            ->set('form', $view)
            ->set('id', $id)
            ->set('name', $name)
            ->set('full_name', $fullName)
            ->set('errors', $form->getErrors())
            ->set('value', $form->getClientData())
            ->set('read_only', $form->isReadOnly())
            ->set('required', $form->isRequired())
            ->set('max_length', $form->getAttribute('max_length'))
            ->set('pattern', $form->getAttribute('pattern'))
            ->set('size', null)
            ->set('label', $form->getAttribute('label'))
            ->set('multipart', false)
            ->set('attr', $form->getAttribute('attr'))
            ->set('types', $types)
        ;
    }
4

2 回答 2

1

我认为您可以尝试构建自己的表单类型。

你可以把你自己的表单类型放到你的包中,你有一个干净的结构,比如(“TestBundle/Form/Type”)。

在此字段类型中,您可以进行所需的更改。

如何在 symfony2 中创建自定义字段类型?

这是一个有用的帖子,显示制作自定义字段类型很热门。

这是一个简短的提示,我希望您能找到一个好的解决方案,并告诉我们它是否有效以及您如何解决它。

于 2012-04-08T08:48:41.760 回答
0

构建了一个辅助函数。

public function fixForm(\Symfony\Component\Form\FormView $form)
{
    foreach($form as &$child)
    {
        $name = $child->get('full_name');
        $id = $child->get('id');
        $matches = array();
        preg_match('/^(?<form>.+)\[(?<ele>.+)\]/', $name, $matches);
        if(isset($matches['ele']))
            $child->set('full_name', $matches['ele']);
        $matches = array();
        preg_match('/^(?<first>.+)_(?<second>.+)/', $id, $matches);
        if(isset($matches['second']))
            $child->set('id', $matches['second']);
    }
    return $form;
}

及其工作。当您需要修复表单时只需调用它

于 2012-04-09T17:20:49.920 回答