我之前有过这项工作,但它已停止使用Symfony 2.7
我想要的是渲染扩展/多个实体选择列表,以便显示多个自定义属性。目标是将选项列为:
{name} - {description}更多信息
所以我用" entity"创建了一个自定义表单类型。作为父母,所以我可以自定义表格渲染
<?php
namespace Study\MainBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ScholarshipEntityType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->setAttribute('dataType', $options['dataType']);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'required' => false,
'dataType' => 'entity'
));
}
public function getAllowedOptionValues(array $options)
{
return array('required' => array(false));
}
public function getParent()
{
return 'entity';
}
public function getName()
{
return 'scholarship_entity';
}
}
我按如下方式呈现类型(它仅基于Twitter Bootstrap包模板):
{% block scholarship_entity_widget %}
{% spaceless %}
{% if expanded %}
{% set label_attr = label_attr|merge({'class': (label_attr.class|default(''))}) %}
{% set label_attr = label_attr|merge({'class': (label_attr.class ~ ' ' ~ (widget_type != '' ? (multiple ? 'checkbox' : 'radio') ~ '-' ~ widget_type : ''))}) %}
{% if expanded %}
{% set attr = attr|merge({'class': attr.class|default(horizontal_input_wrapper_class)}) %}
{% endif %}
{% for child in form %}
{% if widget_type != 'inline' %}
<div class="{{ multiple ? 'checkbox' : 'radio' }}">
{% endif %}
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
{{ form_widget(child, {'horizontal_label_class': horizontal_label_class, 'horizontal_input_wrapper_class': horizontal_input_wrapper_class, 'attr': {'class': attr.widget_class|default('')}}) }}
{{ child.vars.label.name|trans({}, translation_domain) }}
- {{ child.vars.label.description }}
<a href="{{ child.vars.label.link }}" target="_blank">More Information</a>
</label>
{% if widget_type != 'inline' %}
</div>
{% endif %}
{% endfor %}
{{ block('form_message') }}
{% if expanded %}
{% endif %}
{% else %}
{# not being used, just default #}
{{ block('choice_widget_collapsed') }}
{% endif %}
{% endspaceless %}
{% endblock %}
最后,我以另一种形式使用我的自定义类型:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// ...
->add('scholarships', new ScholarshipEntityType(), array(
'class' => 'StudyMainBundle:Scholarship',
'query_builder' => function(EntityRepository $er) use ($options) {
return $er->findAllByOfferingQueryBuilder($options['offering']);
},
'choice_label' => 'entity',
'multiple' => true,
'expanded' => true,
'label' => 'financial.scholarships'
))
;
}
&#34;属性&#34;我的渲染只是实体本身:
/**
* Scholarship
*
* @ORM\Table(name="scholarship")
* @ORM\Entity(repositoryClass="Study\MainBundle\Repository\ScholarshipRepository")
*/
class Scholarship
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
// ...
/**
* Get the Entity object for form rendering
*
* @return \Study\MainBundle\Entity\Scholarship
*/
public function getEntity()
{
return $this;
}
}
不幸的是,看起来我将整个实体传递给Twig并让我访问属性的技巧已经不再适用了。将标签呈现为字符串会有一些变化(我更改了&#39;属性&#39;以及<#39; choice_label&#39;以上为2.7,如果这很重要的话。)
错误:
捕获致命错误:类Study \ MainBundle \ Entity \ Scholarship的对象无法转换为字符串
堆栈追踪:
1. in vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php at line 251 +
2. at ErrorHandler ->handleError ('4096', 'Object of class Study\MainBundle\Entity\Scholarship could not be converted to string', '/var/project/vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php', '251', array('choice' => object(Scholarship), 'key' => '0', 'label' => object(Closure), 'values' => array('2'), 'index' => array('Symfony\Bridge\Doctrine\Form\Type\DoctrineType', 'createChoiceName'), 'attr' => null, 'isPreferred' => array(), 'preferredViews' => array(), 'otherViews' => array(), 'value' => '2', 'nextIndex' => '2'))
in vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php at line 251 +
3. at DefaultChoiceListFactory ::addChoiceView (object(Scholarship), '0', object(Closure), array('2'), array('Symfony\Bridge\Doctrine\Form\Type\DoctrineType', 'createChoiceName'), null, array(), array(), array())
in vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php at line 185
还有其他方法可以实现这一目标吗?
我正在考虑以下事项(但不知道如何做到这些或者是否值得研究其中任何一个):
答案 0 :(得分:2)
如果我正确理解了这个问题,你应该在你的实体中实现__toString()
函数,它将在你的实体的Choice列表中格式化你想要打印的字符串。
例如:
function __toString() {
return sprintf("%s - %s", $this->type, $this->description);
}
答案 1 :(得分:0)
尝试使用方法AbstractType::buildView(FormView, FormInterface, array)
。在那里,您可以访问传递给模板的变量。
我用它来DaterangeType
为两个日期字段声明单独的ID和名称:
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['full_name_0'] = $view->vars['full_name'] . '[0]';
$view->vars['full_name_1'] = $view->vars['full_name'] . '[1]';
$view->vars['id_0'] = $view->vars['id'] . '_0';
$view->vars['id_1'] = $view->vars['id'] . '_1';
}
然后,您可以将这些值作为标准树枝变量访问。