该属性和方法都不存在,并在自定义类中有公共访问FieldType
问题描述:
我在我的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 -%}
就像我添加的两个,因为我只用它们来定制我的按钮。 任何人都可以帮助我吗?
答
修改下面我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,
));
}
}
现在一切工作正常:)
尝试添加'映射=> FALSE'在你的表单生成器?我认为,如果这些参数仅用于视图渲染,只需在表单构建器的'attr'下添加值并在模板构件(不带扩展名)中访问它们即可。 – ejuhjav
是的,但这不是按钮标签的属性。我必须在之间加上一些东西,并在表单构建器中使用'attr',我只能在按钮标签中传递一些东西。当我在'label'中添加内容时,HTML代码被打印出来,并且不能被正确解释。 – user3296316
映射不是SubmitType :-( – user3296316