[英]Additional properties to entity Field Type in a form in Symfony2
在Symfony2中,有沒有辦法將實體中的更多字段映射到從基於實體的表單生成的選擇下拉列表的選項標簽?
我目前有類似的東西:
$builder->add('creditcard', 'entity',
array( 'label' => 'Credit Card',
'required' => true,
'expanded' => false,
'class' => 'Acme\Bundle\Entity\CreditCard',
'property' => 'display_text',
'multiple' => false,
'query_builder' => function(\Acme\Bundle\Repository\CreditCardRepository $er) {
return $er->createQueryBuilder('b');
},
'mapped' => false,
));
這很好用,但我想生成類似的東西:
<option value="id" string_mapped_from_field1="value_of_field1">display_text</option>
謝謝!
好的,如果有人帶着同樣的問題來到這里,這就是我最終所做的:
我創建了一個自定義字段類型(請參閱http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html )
由於我們最終將成為一個實體字段,您要添加:
public function getParent() {
return 'entity';
}
在表格上使用時:
$builder->add('creditcard', new CreditCardFieldType(),
array( 'label' => 'Credit Card',
'required' => true,
'expanded' => false,
'class' => 'Acme\Bundle\Entity\CreditCardCharge',
'property' => 'object',
'multiple' => false,
'query_builder' => function(\Acme\Bundle\Repository\CreditCardChargeRepository $er) {
return $er->createQueryBuilder('b');
},
'mapped' => false,
));
object是添加到包含整個對象的實體的新屬性,因此我添加到實體:
public function getObject()
{
return $this;
}
這樣我們就可以從模板訪問對象了,我們只需要為自己的自定義字段類型創建一個新模板:
{% block creditcard_widget %}
{% spaceless %}
{% if required and empty_value is none and not empty_value_in_choices %}
{% set required = false %}
{% endif %}
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
{% if empty_value is not none %}
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ empty_value|trans({}, translation_domain) }}</option>
{% endif %}
{% if preferred_choices|length > 0 %}
{% set options = preferred_choices %}
{{ block('choice_creditcard_widget_options') }}
{% if choices|length > 0 and separator is not none %}
<option disabled="disabled">{{ separator }}</option>
{% endif %}
{% endif %}
{% set options = choices %}
{{ block('choice_creditcard_widget_options') }}
</select>
{% endspaceless %}
{% endblock creditcard_widget %}
{% block choice_creditcard_widget_options %}
{% spaceless %}
{% for group_label, choice in options %}
{% if choice is iterable %}
<optgroup label="{{ group_label|trans({}, translation_domain) }}">
{% set options = choice %}
{{ block('choice_creditcard_widget_options') }}
</optgroup>
{% else %}
<option value="{{ choice.data.creditcard }}" charge="{{ choice.data.charge }}" {% if choice is selectedchoice(data.creditcard_charges_id) %} selected="selected"{% endif %}>{{ choice.data.text|trans({}, translation_domain) }}</option>
{% endif %}
{% endfor %}
{% endspaceless %}
{% endblock choice_creditcard_widget_options %}
並在config.yml中為twig注冊它:
twig:
form:
resources:
- 'AcmeBundle:Form:creditcardfield.html.twig'
不確定它是最好的解決方案,但它可以解決問題。 希望能幫助到你。
另一種解決方案是在CreditCardCharge實體上聲明函數__toString()
http://symfony.com/doc/current/reference/forms/types/entity.html#property
喜歡 :
public function __toString(){
return $this->data1.'-'.$this->data2;
}
這個函數必須返回一個字符串,用你的邏輯替換我的樣本
並刪除“屬性”選項以默認使用此功能
$builder->add('creditcard', 'entity',
array( 'label' => 'Credit Card',
'required' => true,
'expanded' => false,
'class' => 'Acme\Bundle\Entity\CreditCard',
'multiple' => false,
'query_builder' => function(\Acme\Bundle\Repository\CreditCardRepository $er) {
return $er->createQueryBuilder('b');
},
'mapped' => false,
));
從Symfony 2.7
您可以使用choice_attr
$builder->add('creditcard', 'entity',
array( 'label' => 'Credit Card',
// ...
'choice_attr' => function($object, $key, $index) {
return [
'string_mapped_from_field1' => $object->getValueOfField1()
];
},
));
我把它放在我的empField.php中修復它:
{%- block choice_widget_options -%}
{% for group_label, choice in options %}
{%- if choice is iterable -%}
<optgroup label="{{ group_label|trans({}, translation_domain) }}">
{% set options = choice %}
{{- block('choice_widget_options') -}}
</optgroup>
{%- else -%}
<option value="{{ choice.value }}" data-position="{{ choice.data.position }}" data-avatar="{% path choice.data.avatar, 'reference' %}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice.data.fullname }}</option>
{%- endif -%}
{% endfor %}
{%- endblock choice_widget_options -%}
(它來自:\\ vendor \\ symfony \\ symfony \\ src \\ Symfony \\ Bridge \\ Twig \\ Resources \\ views \\ Form \\ form_div_layout.html.twig)它有效,我不確定它是正確的做但是是的有用。
我試圖用我的實體這個例子,但我有一個問題。 它不檢測頁面加載后是否選擇了值,並且在提交后不會保留。
我覺得這與這一行有關:
<option value="{{ choice.data.creditcard }}" charge="{{ choice.data.charge }}" {% if choice is selectedchoice(data.creditcard_charges_id) %} selected="selected"{% endif %}>{{ choice.data.text|trans({}, translation_domain) }}</option>
我不知道要放什么而不是: data.creditcard_charges_id 。
這是我的看法:
{% block emp_widget %}
{% spaceless %}
{% if required and empty_value is none and not empty_value_in_choices %}
{% set required = false %}
{% endif %}
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
{% if empty_value is not none %}
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ empty_value|trans({}, translation_domain) }}</option>
{% endif %}
{% if preferred_choices|length > 0 %}
{% set options = preferred_choices %}
{{ block('choice_emp_widget_options') }}
{% if choices|length > 0 and separator is not none %}
<option disabled="disabled">{{ separator }}</option>
{% endif %}
{% endif %}
{% set options = choices %}
{{ block('choice_emp_widget_options') }}
</select>
{% endspaceless %}
{% endblock emp_widget %}
{% block choice_emp_widget_options %}
{% spaceless %}
{% for group_label, choice in options %}
{% if choice is iterable %}
<optgroup label="{{ group_label|trans({}, translation_domain) }}">
{% set options = choice %}
{{ block('choice_emp_widget_options') }}
</optgroup>
{% else %}
<option value="{{ choice.data.id }}" data-position="{{ choice.data.position }}" data-avatar="{% path choice.data.avatar, 'reference' %}" {% if choice is selectedchoice(choice.data.id) %} selected="selected"{% endif %}>{{ choice.data.firstname }} {{ choice.data.lastname }}</option>
{% endif %}
{% endfor %}
{% endspaceless %}
{% endblock choice_emp_widget_options %}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.