2016-11-22 4 views
0

私はsymfonyプロジェクトにカスタムFieldTypeExtensionを追加しました。私はカスタムタイプにフィールドタイプを呼び出すプロパティやメソッドのいずれも存在せず、カスタムFieldTypeのクラスでパブリックアクセスを持っていません

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; 
     } 
    } 
} 

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', 
     )); 
    } 
} 

私はglyphiconと私のsubmitTypeをカスタマイズするにはこれが必要私はSubmitTypeを拡張したい、SubmitTypeExtension.phpがどのように見えます。 services.ymlで

{% 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 -%} 

私はサービス

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

を追加しました。しかし、今、私はこの問題は、私は任意のプロパティを持つことができない、で次のエラー

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

を取得私は2つのボタンのように私は私のボタンをカスタマイズするためにそれらを使用して追加しました。 誰でも助けてくれますか?

+0

試みを追加するために働く '=> false'をフォームビルダでマッピングされましたか?これらのパラメータがビューレンダリング用のものであれば、フォームビルダの 'attr'に値を追加し、テンプレートウィジェット(拡張子なし)にそれらの値にアクセスするだけで、これを簡単に実現できます。 – ejuhjav

+0

ええ、これはボタンタグの属性ではありません。私はの間に何かを置く必要があります。フォームビルダーで 'attr'を使うと、ボタンタグ自体に何かしか渡せませんでした。 'label'に内容を追加すると、HTMLコードが出力され、正しく解釈されません。 – user3296316

+0

は、SubmitTypeのための有効なオプションではありません:-( – user3296316

答えて

0

私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, 
     )); 
    } 
} 

これで、すべての罰金:)

関連する問題