【问题标题】:symfony createForm dynamic parameters errorsymfony createForm 动态参数错误
【发布时间】:2020-10-13 15:48:22
【问题描述】:

我在 symfony 5.1 中调用 create form 方法时遇到了一个奇怪的问题。

代码失败:

namespace App\Controller;

use App\Entity\Documents\ventehabitation;
use App\Form\Documents\ventehabitationType;
use App\Entity\UserDocuments;
use App\Entity\Documents\contratdetravail;
use App\Form\Documents\contratdetravailType;
public function fillDocument(DocumentsRepository $docRepo, Request $request, int $id)
    {
            $currentDoc = $docRepo->findBy(["id" => $id]);
            $name = $currentDoc[0]->getDocModelName();
            $docEntity= str_replace("_", "", $name);
            $formEntity = $docEntity."Type::class";
            $docEntity = "App\\Entity\\Documents\\".$docEntity;

            $document = new $docEntity();

            $form = $this->createForm($formEntity, $document, [], "");

错误:无法加载类型“ventehabitationType::class”:类不存在。

如果我在 $formEntity 变量前添加“App\Form\Documents\”,也会出现同样的错误。 但是工作正常如果我在 createForm 参数中明确编写 ventehabitationType::class ,在这种情况下这对我没有帮助,因为我需要动态启动我的表单。

composer.json 信息以备不时之需:

    "autoload": {
        "psr-4": {
            "App\\": "src/",
            "App\\Controller\\Documents\\": "src/Controller/Documents/",
            "App\\Entity\\Documents\\": "src/Entity/Documents/",
            "App\\Form\\Documents\\": "src/Form/Documents"
        }
    },

我的班级确实存在。例如,ventehabitationType 表单类在“App\Form\Documents\”中创建,包含:

namespace App\Form\Documents;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use App\Entity\Documents\ventehabitation;

class ventehabitationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('test', TextType::class, [
                'label' => 'test',
            ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => ventehabitation::class,
        ]);
    }
}

我的实体加载正确,只有createForm的第一个参数有问题。

这似乎是一个菜鸟问题,但我仍然无法弄清楚...... 也许这是一个 PHP 限制?

如果有人能解决问题,请告诉我。

【问题讨论】:

    标签: php class symfony parameters


    【解决方案1】:

    ::class 关键字将根据其命名空间返回具有完全限定类名的字符串。但是,它是编译时操作,因此您不能在运行时使用它。

    由于它只返回一个字符串,这就是 FormFactory 所期望的,您可以使用与 $docEntity 类名相同的方式构建它:

    $formEntity = 'App\Form\Documents\\'.$docEntity.'Type';
    

    【讨论】:

    • 非常感谢,它解决了。我不知道这是一个编译时操作,尽管一旦我阅读了您的评论,这似乎很明显。再次感谢!
    • @ww0k 嗯,它毕竟是一个关键字,所以如果你将它嵌入到一个字符串中,它就不会被评估。很高兴它正在工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 2017-05-19
    • 2013-02-28
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多