为什么所需的属性在我的 Symfony 5 formbuilder 中没有更改为 false?

问题描述

最近我开始通过视频教程和文档学习 Symfony 5。当我使用表单构建器创建表单时,我希望自动填充 slug 字段,但为此我需要从中删除 required 属性。问题是,将 required=>false 发送给构建器不会改变任何东西。我什至尝试为所有字段设置 required=>false 但没有运气。当我转储我返回的内容时,我确实得到了一个包含我传递的值的数组,但它不会生效。

我在互联网上搜索,向我的朋友寻求帮助,但我没有想法。你能帮我解决这个问题吗?

这是代码,如果您需要更多,我可以提供更多。提前致谢。

class AdType extends AbstractType
{

    /**
     * Permet d'avoir la configuration de base d'un champ !
     *
     * @param string $label
     * @param string $placeholder
     * @param array $options
     * @return array
     */
    private function getConfiguration($label,$placeholder,$options = []) {
        return array_merge_recursive([
            'label' => $label,'attr' => [
                'placeholder' => $placeholder,]
        ],$options);

    }

    public function buildForm(FormBuilderInterface $builder,array $options)
    {
        $builder
            ->add('title',TextType::class,$this->getConfiguration("Titre","Tapez un super titre pour votre annonce"))
            ->add(
                'slug',$this->getConfiguration("Adresse web","Tapez l'adresse web (automatique)"),[
                    'required' => false,]
            )
            ->add('coverImage',UrlType::class,$this->getConfiguration("URL de l'image principale","Donnez l'adresse d'une image qui donne vraiment envie"))
            ->add('introduction',$this->getConfiguration("Introduction","Donnez une description globale de l'annonce"))
            ->add('content',TextareaType::class,$this->getConfiguration("Description détaillée","Tapez une description qui donne vraiment envie de venir chez vous"))
            ->add('rooms',IntegerType::class,$this->getConfiguration("Nombre de chambres","Le nombre de chambres disponibles"))
            ->add('price',MoneyType::class,$this->getConfiguration("Prix par nuit","Indiquez le prix que vous voulez pour une nuit"))
            ->add(
                'images',CollectionType::class,[
                      'entry_type' => ImageType::class,'allow_add' => true
                ]
            )
        ;
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...