Access form.vars.value in Symfony subform using Twig

I am working on a Symfony 2.7 WebApp and I would like to use a custom Form Widget for one of the entites. The Widgets needs to access the form.vars.value . This works fine as long as the Widget is uses within the main form. But when using the Widget in a subform, form.vars.value is empty.

The classes used within the form:

class AdressBookEntry { 
    // The main phone number of this contact: Type PhoneNumber
    protected $mainPhoneNumber;
    //...getter and setter for mainPhoneNumber

    // An array of Addresses
    protected $addresses;
    //...getter and setter for addresses

    ...
}

class Address { 
    // The phone number of this address: Type PhoneNumber
    protected $phoneNumber;
    //...getter and setter for phoneNumber

    ...
}

class PhoneNumber {
    ...
}

The custom Form Types for theses classes:

// Custom FormType for AddressBookEntries
class AdressBookEntryType extends AbstractType {
    ...

    public function buildForm(FormBuilderInterface $builder, array $options) {  
        // Type 'phone_number_edit' is registered in services.yml
        $builder
            ->add('mainPhoneNumber', 'phone_number_edit', array(
                'label' => '...',
                ... 
            ))  

            ->add('addresses', 'collection', array(
                'label' => '...',
                ... 
            ));
    }
}


// Custom FormType for Address
class AddressType extends AbstractType {
    ...

    public function buildForm(FormBuilderInterface $builder, array $options) {  
        $builder
            ->add('mainPhoneNumber', 'phone_number_edit', array(
                'label' => '...',
                ... 
            ))  

            ...;
    }
}

The custom Widget for the PhoneNumberEdit

{% block phone_number_edit_widget %}
    ...
    {{ dump(form.vars.value) }}
    ...

The PhoneNumberEdit for the main form (representing the AddressBookEntry ) works fine. The dump statement shows the content of the assigned PhoneNumber object.

Within the Subform of the addresses collection however, the form.vars.value variable is empty. The dump shows just "" .

So, how do I access form.vars.value within the subform? How can the widget recognize wether it is being uses within the main form or a subform?

UPDATE:

Some additional information as asked in the comments:

@Jeet: As described before the dump shows an empty value/string: ""

@DOZ: This is the Twig code:

{{ form_start(form) }}
    {{ form_errors(form) }}         
    {{ form_row(form.name) }}

    {{ form_widget(mainPhoneNumber) }}

    <ul data-prototype"{{ _self.addressItem(form.addresses.vars.prototype)|e('html_attr') }}" >
        {% for address in form.addresses %}
            {{ _self.addressItem(address) }}
        {% endfor %}
    </u>

    ...
{{ form_end(form) }}

{% macro addressItem(address) %}
    <li>
        {{ form_widget(address.phoneNumber) }}
        ...
    </li>
{% endmacro %}  

使用值而不是form.vars.value

{% block phone_number_edit_widget %}
    ...
    {{ dump(value) }}
    ...
链接地址: http://www.djcxy.com/p/81402.html

上一篇: 形式主题和Symfony收藏

下一篇: 使用Twig在Symfony子窗体中访问form.vars.value