【问题标题】:Sylius v1.0.0 simple form extensionSylius v1.0.0 简单表单扩展
【发布时间】:2017-10-11 22:00:55
【问题描述】:

我在扩展表单时遇到问题。我只想更改欧盟风格的生日格式(dd.MM.yyyy)。所以我创建了 src/AppBundle/Form/Extension/CustomerProfileTypeExtension.php

<?php

namespace AppBundle\Form\Extension;

use Sylius\Bundle\CustomerBundle\Form\Type\CustomerProfileType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

final class CustomerProfileTypeExtension extends AbstractTypeExtension
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{

// Change to EU Style dd.MM.yyyy
$builder->add('birthday', BirthdayType::class, [
'format' => 'dd.MM.yyyy',
]);
}

/**
* {@inheritdoc}
*/
public function getExtendedType()
{
return CustomerProfileType::class;
}
}

我将 AppBundle/Resources/config/services.yml 更改为

services:
app.form.extension.type.customer_profile:
class: AppBundle\Form\Extension\CustomerProfileTypeExtension
tags:
- { name: form.type_extension, extended_type: Sylius\Bundle\CustomerBundle\Form\Type\CustomerProfileType } 

但没有变化。缓存清除对我没有帮助! 我也只是像在 sylius 示例中那样进行了更改,以删除性别字段或将 required 更改为 true - 它不起作用。

有什么想法/帮助吗?

迈克

【问题讨论】:

    标签: sylius


    【解决方案1】:

    我刚刚对此进行了测试,但由于您缺少以下行而引发了错误

    use Symfony\Component\Form\Extension\Core\Type\BirthdayType;

    事实上,删除

    use Symfony\Component\Form\Extension\Core\Type\TextType;

    因为它没有被使用,所以用生日类型替换它

    一旦我添加了它,它就可以正常工作。

    约翰

    【讨论】:

    • 嗨约翰,我不知道为什么......它不适合我。我也尝试这样做以注册表格。没有成功。可以肯定的是……我写的都是正确的?我的意思是路径,没有别的?配置没有变化?所以这也是我的 services.yml 中的 1by1。还是有什么遗漏? THX 迈克
    • 啊,发现你所做的与我昨晚所做的有所不同。将服务定义放入 app/config/services.yml 并尝试一下。
    【解决方案2】:

    好的。 - 使其正确:

    acme/src/AppBundle/Form/Extension/CustomerProfileTypeExtension.php

    <?php
    
    namespace AppBundle\Form\Extension;
    
    use Sylius\Bundle\CustomerBundle\Form\Type\CustomerProfileType;
    use Symfony\Component\Form\AbstractTypeExtension;
    use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
    use Symfony\Component\Form\FormBuilderInterface;
    
    final class CustomerProfileTypeExtension extends AbstractTypeExtension
    {
    /**
    * {@inheritdoc}
    */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    
    // Change to EU Style dd.MM.yyyy
    $builder->add('birthday', BirthdayType::class, [
    'format' => 'dd.MM.yyyy',
    'required' => true,
    ]);
    }
    
    /**
    * {@inheritdoc}
    */
    public function getExtendedType()
    {
    return CustomerProfileType::class;
    }
    }
    

    acme/src/AppBundle/Resources/config/services.yml

    services:
    app.form.extension.type.customer_profile:
    class: AppBundle\Form\Extension\CustomerProfileTypeExtension
    tags:
    - { name: form.type_extension, extended_type: Sylius\Bundle\CustomerBundle\Form\Type\CustomerProfileType } 
    

    相同 - 没有变化 - 如果我改到这里。 acme/app/config/services.yml

    字段没有变化。没有错误 - 当我清除缓存时也没有。

    【讨论】:

    • 我也遇到了同样的问题,你找到解决办法了吗?
    • 是的,我忘记在我的 config.yml 中导入 services.yml - { resource: "services.yml" }
    【解决方案3】:

    解决方案:我忘记在我的 config.yml 中导入 services.yml ´- { resource: "services.yml" }´

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-08
      • 2021-08-08
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多