Symfony 2.8在集合内部形成子集合,但未正确显示

时间:2017-06-26 10:48:28

标签: php forms symfony twig symfony-2.8

我目前正在尝试制作允许更新文档的表单。文档可能有0-n documentLegs,每个documentLeg可能有一个leadPassenger和0-n乘客。每条腿的乘客都与其他腿分开。

以下是我的表单类:

class DocumentType extends AbstractResourceType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('currencyCode', TextType::class, ['label' => 'model.document.currency_code'])
            ->add('reservationId', TextType::class, ['label' => 'model.document.reservation_id'])
            ->add('sum', NumberType::class, ['label' => 'model.document.sum'])
            ->add('tax', NumberType::class, ['label' => 'model.document.tax'])
            ->add('tripFolder', TextType::class, ['label' => 'model.document.trip_folder'])
            ->add('documentCustomer', DocumentCustomerType::class)
            ->add('documentAdditionalServices', CollectionType::class, ['entry_type' => DocumentAdditionalServiceType::class])
            ->add('documentLegs', CollectionType::class, ['entry_type' => DocumentLegType::class])
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        parent::configureOptions($resolver);
        $resolver->setDefault('translation_domain', 'document');
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'oj_document';
    }
}

class DocumentLegType extends AbstractResourceType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('airportFromIata', TextType::class, ['label' => 'model.leg.airport_from_iata'])
            ->add('airportFromIcao', TextType::class, ['label' => 'model.leg.airport_from_icao'])
            ->add('airportFromName', TextType::class, ['label' => 'model.leg.airport_from_name'])
            ->add('airportToIata', TextType::class, ['label' => 'model.leg.airport_to_iata'])
            ->add('airportToIcao', TextType::class, ['label' => 'model.leg.airport_to_icao'])
            ->add('airportToName', TextType::class, ['label' => 'model.leg.airport_to_name'])
            ->add('cityFromName', TextType::class, ['label' => 'model.leg.city_from_name'])
            ->add('cityToName', TextType::class, ['label' => 'model.leg.city_to_name'])
            ->add('price', NumberType::class, ['label' => 'model.leg.price'])
            ->add('publicPrice', NumberType::class, ['label' => 'model.leg.public_price'])
            ->add('tax', NumberType::class, ['label' => 'model.leg.tax'])
            ->add('startedAtUtc', DateTimePickerType::class, ['label' => 'model.leg.started_at_utc'])
            ->add('endedAtUtc', DateTimePickerType::class, ['label' => 'model.leg.ended_at_utc'])
            ->add('leadPassenger', DocumentCustomerType::class)
            ->add('passengers', CollectionType::class, ['entry_type' => DocumentCustomerType::class])
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        parent::configureOptions($resolver);
        $resolver->setDefault('translation_domain', 'document');
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'oj_document_leg';
    }
}

class DocumentCustomerType extends AbstractResourceType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('address', TextType::class, ['label' => 'model.customer.address'])
            ->add('city', TextType::class, ['label' => 'model.customer.city'])
            ->add('company', TextType::class, ['label' => 'model.customer.company'])
            ->add('country', TextType::class, ['label' => 'model.customer.country'])
            ->add('firstname', TextType::class, ['label' => 'model.customer.firstname'])
            ->add('gender', TextType::class, ['label' => 'model.customer.gender'])
            ->add('lastname', TextType::class, ['label' => 'model.customer.lastname'])
            ->add('postalCode', TextType::class, ['label' => 'model.customer.postal_code'])
            ->add('email', EmailType::class, ['label' => 'model.customer.email'])
            ->add('phone', TextType::class, ['label' => 'model.customer.phone'])
            ->add('passportNumber', TextType::class, ['label' => 'model.customer.passport_number'])
            ->add('birthdate', DatePickerType::class, ['label' => 'model.customer.birthdate'])
            ->add('passportExpirationDate', DatePickerType::class, ['label' => 'model.customer.passport_expiration_date'])
            ->add('nationality', TextType::class, ['label' => 'model.customer.nationality'])
            ->add('honorific', TextType::class, ['label' => 'model.customer.honorific'])
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        parent::configureOptions($resolver);
        $resolver->setDefault('translation_domain', 'document');
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'oj_document_customer';
    }
}

在此之后,这是表格模板,使用Twig:

{% form_theme form with [
':Form:horizontalLayout.html.twig',
':Form:widgetHorizontal.html.twig',
] %}

{% trans_default_domain 'document' %}

{{ set_globals({
    label_col : 3,
    group_col : 9
}) }}

<div class="section section-heading">
    <h2>{{ 'model.section.document'|trans }}</h2>
</div>
<section>
    {{ form_row(form.reservationId) }}
    {{ form_row(form.tripFolder) }}
    {{ form_row(form.currencyCode) }}
    {{ form_row(form.sum) }}
    {{ form_row(form.tax) }}
</section>
<div class="section section-heading">
    <h2>{{ 'model.section.document_customer'|trans }}</h2>
</div>
<section>
    {{ form_widget(form.documentCustomer) }}
</section>
<div class="section section-heading">
    <h2>{{ 'model.section.additional_services'|trans }}</h2>
</div>
<section>
    {% for service in form.documentAdditionalServices %}
        <div>{{ loop.index }}</div>
        {{ form_row(service.name) }}
        {{ form_row(service.description) }}
        {{ form_row(service.price) }}
        {{ form_row(service.publicPrice) }}
    {% endfor %}
</section>
<div class="section section-heading">
    <h2>{{ 'model.section.document_legs'|trans }}</h2>
</div>
<section>
    <div class="panel-group" id="legAccordion" role="tablist" aria-multiselectable="true">
        {% for leg in form.documentLegs %}
            <div class="panel panel-default">
                <div class="panel-heading" role="tab" id="headerLeg{{ loop.index }}">
                    <h4 class="panel-title">
                        <a role="button" data-toggle="collapse" data-parent="#legAccordion" href="#collapseLeg{{ loop.index }}" aria-expanded="true" aria-controls="#collapseLeg{{ loop.index }}">
                            {{ 'model.accordion.leg_header'|trans({'%number%': loop.index}, 'document') }}
                        </a>
                    </h4>
                </div>
                <div id="collapseLeg{{ loop.index }}" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headerLeg{{ loop.index }}">
                    <div class="panel-body">
                        {{ form_row(leg.airportFromName) }}
                        {{ form_row(leg.airportFromIcao) }}
                        {{ form_row(leg.airportFromIata) }}
                        {{ form_row(leg.airportToIata) }}
                        {{ form_row(leg.airportToIata) }}
                        {{ form_row(leg.airportToIata) }}
                        {{ form_row(leg.cityFromName) }}
                        {{ form_row(leg.cityToName) }}
                        {{ form_row(leg.price) }}
                        {{ form_row(leg.publicPrice) }}
                        {{ form_row(leg.tax) }}
                        {{ form_row(leg.startedAtUtc) }}
                        {{ form_row(leg.endedAtUtc) }}
                        <div class="panel-group" id="leadPaxAccordion" role="tablist" aria-multiselectable="true">
                            <div class="panel panel-default">
                                <div class="panel-heading" role="tab" id="headerLeadPax">
                                    <h4 class="panel-title">
                                        <a role="button" data-toggle="collapse" data-parent="#leadPaxAccordion" href="#collapseLeadPax" aria-expanded="true" aria-controls="#collapseLeadPax">
                                            {{ 'model.section.lead_passenger'|trans({}, 'document') }}
                                        </a>
                                    </h4>
                                </div>
                                <div id="collapseLeadPax" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headerLeadPax">
                                    <div class="panel-body">
                                        {{ form_row(leg.leadPassenger.firstname) }}
                                        {{ form_row(leg.leadPassenger.lastname) }}
                                        {{ form_row(leg.leadPassenger.company) }}
                                        {{ form_row(leg.leadPassenger.address) }}
                                        {{ form_row(leg.leadPassenger.city) }}
                                        {{ form_row(leg.leadPassenger.postalCode) }}
                                        {{ form_row(leg.leadPassenger.country) }}
                                        {{ form_row(leg.leadPassenger.email) }}
                                        {{ form_row(leg.leadPassenger.phone) }}
                                        {{ form_row(leg.leadPassenger.passportNumber) }}
                                        {{ form_row(leg.leadPassenger.passportExpirationDate) }}
                                        {{ form_row(leg.leadPassenger.nationality) }}
                                        {{ form_row(leg.leadPassenger.honorific) }}
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="panel-group" id="passengerAccordion" role="tablist" aria-multiselectable="true">
                            {% for passenger in leg.passengers %}
                            <div class="panel panel-default">
                                <div class="panel-heading" role="tab" id="headerPassenger{{ loop.index }}">
                                    <h4 class="panel-title">
                                        <a role="button" data-toggle="collapse" data-parent="#passengerAccordion" href="#collapsePassenger{{ loop.index }}" aria-expanded="true" aria-controls="#collapsePassenger{{ loop.index }}">
                                            {{ 'model.accordion.passenger'|trans({'%number%': loop.index}, 'document') }}
                                        </a>
                                    </h4>
                                </div>
                                <div id="collapsePassenger{{ loop.index }}" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headerPassenger{{ loop.index }}">
                                    <div class="panel-body">
                                        {{ form_row(passenger.firstname) }}
                                        {{ form_row(passenger.lastname) }}
                                        {{ form_row(passenger.company) }}
                                        {{ form_row(passenger.address) }}
                                        {{ form_row(passenger.city) }}
                                        {{ form_row(passenger.postalCode) }}
                                        {{ form_row(passenger.country) }}
                                        {{ form_row(passenger.email) }}
                                        {{ form_row(passenger.phone) }}
                                        {{ form_row(passenger.passportNumber) }}
                                        {{ form_row(passenger.passportExpirationDate) }}
                                        {{ form_row(passenger.nationality) }}
                                        {{ form_row(passenger.honorific) }}
                                    </div>
                                </div>
                            </div>
                            {% endfor %}
                        </div>
                    </div>
                </div>
            </div>
        {% endfor %}
    </div>
</section>

{{ form_row(form._token) }}

现在,问题。我们假设我有2个documentLegs。第一个documentLeg有2个passengers和一个leadPassenger。第二站有3名乘客和一名领先乘客(第一站不同)。

我只能在2°腿上获得第三名乘客。首先不会生成第一个和第一个。我以2为例,但它适用于n。

如果有人有想法。提前谢谢。

1 个答案:

答案 0 :(得分:0)

HTML显示问题。与崩溃的ids冲突。