且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Symfony2中表单中实体字段类型的其他属性

更新时间:2023-11-19 17:13:46

好,如果有人带着相同的问题来到这里,这就是我最后要做的事情:

Ok, in case somebody gets here with the same question, this is what I've done in the end:

我创建了一个自定义字段类型(请参见 http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html

I've created a custom field type (see http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html)

由于我们最终将成为实体字段,因此您想添加:

Since we is going to be an entity field in the end, you want to add:

    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 is a new property added to the entity that contains the whole object, so I added to the entity:

public function getObject()
{
    return $this;
}

这样,我们可以从模板访问对象,我们只需要为我们自己的自定义字段类型创建一个新模板:

This way we can access to the object from the template, we just need to create a new template for our own custom field type:

{% 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中将其注册为小树枝:

And register it for twig in your config.yml:

twig:
    form:
        resources:
            - 'AcmeBundle:Form:creditcardfield.html.twig'

不确定这是***的解决方案,但是可以解决问题。希望对您有所帮助。

Not sure it is the best solution but it does the trick. Hope it helps.