0
我在我的symfony項目中添加了一個自定義的FieldTypeExtension。我想延長SubmitType中,SubmitTypeExtension.php樣子:該屬性和方法都不存在,並在自定義類中有公共訪問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',
));
}
}
我需要這個定製我的submitType用glyphicon。在services.yml我已經添加了服務
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".
的問題是,我不能有任何財產
{% 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 -%}
就像我添加的兩個,因爲我只用它們來定製我的按鈕。 任何人都可以幫助我嗎?
嘗試添加'映射=> FALSE'在你的表單生成器?我認爲,如果這些參數僅用於視圖渲染,只需在表單構建器的'attr'下添加值並在模板構件(不帶擴展名)中訪問它們即可。 – ejuhjav
是的,但這不是按鈕標籤的屬性。我必須在之間加上一些東西,並在表單構建器中使用'attr',我只能在按鈕標籤中傳遞一些東西。當我在'label'中添加內容時,HTML代碼被打印出來,並且不能被正確解釋。 – user3296316
映射不是SubmitType :-( – user3296316