dsg7513 2016-11-22 21:37
浏览 39
已采纳

属性和方法之一都不存在,并且在自定义FieldType中的类中具有公共访问权限

I added an custom FieldTypeExtension in my symfony project. I want to extend the SubmitType, the SubmitTypeExtension.php looks like:

class SubmitTypeExtension extends AbstractTypeExtension
{
    /**
     * Returns the name of the type being extended.
     *
     * @return string The name of the type being extended
     */
    public function getExtendedType()
    {
        return SubmitType::class;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefined(array('button_image','button_color'));
    }

    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        if (isset($options['button_image'])) {
            $parentData = $form->getParent()->getData();

            $buttonImage = null;
            if (null !== $parentData) {
                $accessor = PropertyAccess::createPropertyAccessor();
                $buttonImage = $accessor->getValue($parentData, $options['button_image']);
            }

            $view->vars['button_image'] = $buttonImage;
        }
        if (isset($options['button_color'])) {
            $parentData = $form->getParent()->getData();

            $buttonColor = null;
            if (null !== $parentData) {
                $accessor = PropertyAccess::createPropertyAccessor();
                $buttonColor = $accessor->getValue($parentData, $options['button_color']);
            }

            $view->vars['button_color'] = $buttonColor;
        }
    }
}

I call the field type in an custom Type:

class VereinType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class)
            ->add('save', SubmitType::class, array(
                'label' => 'Speichern',
                'button_image' => 'check',
                'button_color' => 'rgba(13, 135, 13, 1)'
            ))
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'TeilnehmerBundle\Entity\Verein',
        ));
    }
}

I need this to customize my submitType with a glyphicon.

{% extends 'bootstrap_3_layout.html.twig' %}

{%- block submit_widget -%}
    {% set attr = attr|merge({class: (attr.class|default('btn-default') ~ ' btn btn-lg btn-sm')|trim}) %}
    {%- if label is empty -%}
        {%- if label_format is not empty -%}
            {% set label = label_format|replace({
            '%name%': name,
            '%id%': id,
            }) %}
        {%- else -%}
            {% set label = name|humanize %}
        {%- endif -%}
    {%- endif -%}
    <button type="{{ type|default('submit') }}" {{ block('button_attributes') }}><span class="glyphicon glyphicon-{{ button_image }}" aria-hidden="true" style="color: {{ button_color }};"></span><b>{{ translation_domain is same as(false) ? label : label|trans({}, translation_domain) }}</b></button>
{%- endblock submit_widget -%}

In the services.yml I've added the service

  app.submit_type_extension:
    class: TeilnehmerBundle\Form\Extension\SubmitTypeExtension
    tags:
      - { name: form.type_extension, extended_type: Symfony\Component\Form\Extension\Core\Type\SubmitType }

But now I get the following error

Neither the property "check" nor one of the methods "getCheck()", "check()", "isCheck()", "hasCheck()", "__get()" exist and have public access in class "TeilnehmerBundle\Entity\Verein". 

The problem is, I can't have any property like the two I've added because I only use them to customize my buttons. Can anyone help me?

  • 写回答

1条回答 默认 最新

  • doufan9805 2016-11-23 09:29
    关注

    Modified following in my SubmitTypeExtension:

    class SubmitTypeExtension extends AbstractTypeExtension
    {
        /**
         * Returns the name of the type being extended.
         *
         * @return string The name of the type being extended
         */
        public function getExtendedType()
        {
            return SubmitType::class;
        }
    
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefined(array('button_image','button_color'));
        }
    
        public function buildView(FormView $view, FormInterface $form, array $options)
        {
            if(isset($options['button_image']))
                $view->vars['button_image'] = $options['button_image'];
            else
                $view->vars['button_image'] = NULL;
    
            if(isset($options['button_color']))
                $view->vars['button_color'] = $options['button_color'];
            else
                $view->vars['button_color'] = NULL;
        }
    
        /**
         * @param OptionsResolverInterface $resolver
         */
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'button_image' => null,
                'button_color' => null,
            ));
        }
    }
    

    Now all works fine :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择