【问题标题】:Sylius custom fields are not validated in formSylius 自定义字段未在表单中验证
【发布时间】:2017-01-14 08:22:49
【问题描述】:

我正在覆盖注册页面(覆盖客户对象);我添加了“类型”,这是一个展开的 ChoiceType(3 个单选按钮),并且添加了 defaultAddress 字段(我在其中添加了 3 个字段)。 当我显示表单时,所有这些字段都有一个红星表示有必填项,但是当我提交表单时,如果我没有在这些字段中输入任何内容,无论如何都会提交表单并且我有一个数据库错误,因为这些字段为空。

这是我的代码:

CustomerRegistrationTypeExtension.php:

$builder->add('type', ChoiceType::class, [
                'choices' => array('Particulier' => Customer::TYPE_PARTICULIER, 'Professionnel' => Customer::TYPE_PRO, 'Projet à but non lucratif' => Customer::TYPE_PROJET),
                'expanded' => true,
                'label' => 'Vous êtes',
                'choice_attr' => array('onclick' => 'alert(\"click\")')
        ])
        ->add('siren', TextType::class)
        ->add('denomination', TextType::class)
        ->add('defaultAddress', AddressType::class);

AddressTypeExtension.php

$builder->add('showOnMap', CheckboxType::class)
                ->add('geocodeLat', HiddenType::class)
                ->add('geocodeLng', HiddenType::class);

_address.html.twig:

{{ form_row(form.showOnMap, {'label' : 'address.showMap.label'}) }}
        {{ form_row(form.geocodeLat)}}
        {{ form_row(form.geocodeLng)}}

_form.html.twig

{{ form_row(form.type) }}

有什么想法吗?

谢谢!

【问题讨论】:

    标签: forms symfony sylius


    【解决方案1】:

    字段附近的红色星号只是一个 UI 功能。要要求某些字段,您必须指定它们的验证配置。查看Symfony validation documentation 以获取所需信息,所有这些都应该在 Sylius 中完美运行;)

    一件重要的事情 - 定义约束时记得在groups 参数中设置sylius,它是Sylius 中的默认验证组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-26
      • 2019-10-04
      • 2014-04-24
      • 1970-01-01
      相关资源
      最近更新 更多