Nested forms
-
Hello
In my Gravity pdf custom template, I use the following code to output values from the nested form (repeater field).<?php foreach ( $form_data['repeater'][226] as $data ): ?> <!-- The sub-field IDs will be unique to your repeater field --> Email: <?= $data[2] ?> <?php endforeach; ?>
But an error is issued.
Warning: Invalid argument supplied for foreach() in....
What’s wrong?
-
Hi there,
The correct way to handle the Gravity Perks Nested Forms add-on in custom PDF templates can be found in our documentation: https://docs.gravitypdf.com/v6/developers/php-form-data-array#nested-forms
You can see the full structure of your entry’s $form_data by using the
?data=1
helper parameter, which will help you better understand what information is available to output and the array structure.Please let me know if you’ve any further questions.
Hi Jake
I just gave an example from your documentation. But, as you can see, it causes an error. Are there any more examples?You gave an example of using the native Gravity Forms Repeater field in a custom template, but also said you are using Gravity Perks Nested Forms add-on which is two separate field types and are handled two separate ways.
If you post your $form_data structure when using the ?data=1 helper parameter I can provide you a code sample specifically for your form.
I don’t understand what needs to be done(
I have a parent form with the nested form field 226. What’s next?
This is parent form-
Array ( [form_id] => 138 [entry_id] => 195635 [form_title] => НС-КЛАССИЧЕСКИЙ [form_description] => [date_created] => 18/11/2021 [date_created_usa] => 11/18/2021 [misc] => Array ( [date_time] => 2021-11-18 09:41:50 [time_24hr] => 09:41 [time_12hr] => 9:41дп [is_starred] => 0 [is_read] => 1 [ip] => 185.116.202.161 [source_url] => https://www.bsoinsur.online/ns-klassicheskij/ [post_id] => [currency] => RUB [payment_status] => [payment_date] => [transaction_id] => [payment_amount] => [is_fulfilled] => [created_by] => 1 [transaction_type] => [user_agent] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 [status] => active ) [field] => Array ( [86.Номер полиса] => 0001-012300- [86] => 0001-012300- [Номер полиса] => 0001-012300- [194.Полис] => НС-КЛАССИЧЕСКИЙ [194] => НС-КЛАССИЧЕСКИЙ [Полис] => НС-КЛАССИЧЕСКИЙ [194.Полис_name] => НС-КЛАССИЧЕСКИЙ [194_name] => НС-КЛАССИЧЕСКИЙ [Полис_name] => НС-КЛАССИЧЕСКИЙ [176.Полис действует?] => Да [176] => Да [Полис действует?] => Да [176.Полис действует?_name] => Да [176_name] => Да [Полис действует?_name] => Да [131.Бланк, серия] => НС [131] => НС [Бланк, серия] => НС [228.Программа] => 1 [228] => 1 [Программа] => [228.Программа_name] => 24 часа в сутки [228_name] => 24 часа в сутки [Программа_name] => 24 часа в сутки [79.Срок страхования] => 12 мес. [79] => 12 мес. [Срок страхования] => [79.Срок страхования_name] => 12 мес. [79_name] => 12 мес. [Срок страхования_name] => 12 мес. [27.Дата начала договора] => 20.11.2021 [27] => 20.11.2021 [Дата начала договора] => 20.11.2021 [138.Дата окончания] => [138] => [Дата окончания] => [227.Страховые риски] => Array ( [0] => 0.45 ) [227] => Array ( [0] => 0.45 ) [Страховые риски] => Array ( [0] => 0.45 ) [227.Страховые риски_name] => Array ( [0] => ?Телесное повреждение (травма) в результате несчастного случая? ) [227_name] => Array ( [0] => ?Телесное повреждение (травма) в результате несчастного случая? ) [Страховые риски_name] => Array ( [0] => ?Телесное повреждение (травма) в результате несчастного случая? ) [195.Страхователь] => 1 [195] => 1 [Страхователь] => 1 [195.Страхователь_name] => 1 [195_name] => 1 [Страхователь_name] => 1 [63.Дата рождения Страхователя] => 06.11.1991 [63] => 06.11.1991 [Дата рождения Страхователя] => 06.11.1991 [6.Фамилия Страхователя] => Петров [6] => Петров [Фамилия Страхователя] => Петров [4.Имя Страхователя] => Алексей [4] => Алексей [Имя Страхователя] => Алексей [5.Отчество Страхователя] => Иванович [5] => Иванович [Отчество Страхователя] => Иванович [99.Документ] => паспорт [99] => паспорт [Документ] => паспорт [8.Серия документа] => 2222 [8] => 2222 [Серия документа] => 2222 [9.Номер документа] => 333333 [9] => 333333 [Номер документа] => 333333 [10.Дата выдачи] => 04.11.2019 [10] => 04.11.2019 [Дата выдачи] => 04.11.2019 [11.Кем выдан] => Отд. Филевский парк ОУФМС России по г. Москве в ЗАО-2 [11] => Отд. Филевский парк ОУФМС России по г. Москве в ЗАО-2 [Кем выдан] => Отд. Филевский парк ОУФМС России по г. Москве в ЗАО-2 [38.Адрес места жительства (регистрации)] => 141011, Московская обл, г Мытищи, ул Бакунинская, д 3, кв 2 [38] => 141011, Московская обл, г Мытищи, ул Бакунинская, д 3, кв 2 [Адрес места жительства (регистрации)] => 141011, Московская обл, г Мытищи, ул Бакунинская, д 3, кв 2 [28.Телефон] => (792) 550-2244 [28] => (792) 550-2244 [Телефон] => (792) 550-2244 [208.Email] => [email protected] [208] => [email protected] [Email] => [email protected] [220.Наименование] => test [220] => test [Наименование] => test [221.ОГРН] => 888999888 [221] => 888999888 [ОГРН] => 888999888 [222.ИНН/КПП] => 888777666655 [222] => 888777666655 [ИНН/КПП] => 888777666655 [223.Адрес юрлица] => Москва, юрлицо [223] => Москва, юрлицо [Адрес юрлица] => Москва, юрлицо [224.Банковские реквизиты] => Рс/ в банке [224] => Рс/ в банке [Банковские реквизиты] => Рс/ в банке [202.Застрахованный и Страхователь - одно лицо] => [202] => [Застрахованный и Страхователь - одно лицо] => [202.Застрахованный и Страхователь - одно лицо_name] => [202_name] => [Застрахованный и Страхователь - одно лицо_name] => [226.Список застрахованных] => [226] => [Список застрахованных] => [225.Страховая премия] => 9000 [225] => 9000 [Страховая премия] => 9000 [75.Логин] => admin_one [75] => admin_one [Логин] => admin_one [151.Создан] => 18.11.2021 [151] => 18.11.2021 [Создан] => 18.11.2021 [140.Территория страхования] => Российская Федерация [140] => Российская Федерация [Территория страхования] => Российская Федерация [190.Подразделение] => [190] => [Подразделение] => [149.Страхователь полностью] => [149] => [Страхователь полностью] => [150.Вид страхования] => добровольное страхование от несчастных случаев и болезней [150] => добровольное страхование от несчастных случаев и болезней [Вид страхования] => добровольное страхование от несчастных случаев и болезней [196.Страховая сумма] => [196] => [Страховая сумма] => [198.Программа] => [198] => [199.Страхователь] => 1 [199] => 1 [201.Срок страхования] => [201] => [207.Год оформления] => [207] => [Год оформления] => [217.Агент] => SuperVisor [217] => SuperVisor [Агент] => SuperVisor [219.Подписант] => Зам. генерального директора по страхованию Козина Л.А. [219] => Зам. генерального директора по страхованию Козина Л.А. [Подписант] => Зам. генерального директора по страхованию Козина Л.А. [210.Доверенность] => № 01-15/12 от 15.12.2020 [210] => № 01-15/12 от 15.12.2020 [Доверенность] => № 01-15/12 от 15.12.2020 [218.Регион] => [218] => [Регион] => [209.Статус оплаты] => 0 [209] => 0 [Статус оплаты] => 0 [209.Статус оплаты_name] => ожидание [209_name] => ожидание [Статус оплаты_name] => ожидание [211.Дата лицензии] => 25.03.2020 [211] => 25.03.2020 [Дата лицензии] => 25.03.2020 [212.Дата правил] => 01.06.2020 г. [212] => 01.06.2020 г. [Дата правил] => 01.06.2020 г. [213.Код продукта] => 012001 [213] => 012001 [Код продукта] => 012001 [230.Риск инвалидность] => 0 [230] => 0 [Риск инвалидность] => 0 [231.Сумма рисков] => 0.45 [231] => 0.45 [Сумма рисков] => 0.45 ) [pages] => Array ( ) [html_id] => Array ( [41] => СТРАХОВАТЕЛЬ: [46] => ЗАСТРАХОВАННЫЙ: ) [field_descriptions] => Array ( [86] => [194] => [176] => [131] => [228] => [79] => [27] => [138] => [227] => [41] => [195] => [63] => [6] => [4] => [5] => [99] => [8] => [9] => [10] => [11] => [38] => [28] => [208] => [220] => [221] => [222] => [223] => [224] => [202] => [46] => [226] => [225] => [75] => [151] => [140] => [190] => [149] => [150] => [196] => [198] => [199] => [201] => [207] => [217] => [219] => [210] => [218] => [209] => [211] => [212] => [213] => [230] => [231] => ) [html] => Array ( [0] => ЗАСТРАХОВАННЫЙ: ) )
Thanks. Assuming the Nested Form field is ID226, you would use the following code in your template:
$nested_ids = explode( ',', $form_data['field'][226] ); foreach ( $nested_ids as $id ) { $nested_form_data = GPDFAPI::get_form_data( $id ); if ( ! is_wp_error( $nested_form_data ) ) { /* * Output required content for this entry using the $nested_form_data array, * which has the same structure as $form_data (but for the current nested form entry) */ echo $nested_form_data['field'][2]; // output field ID 2 in the child form } }
The above code is a slightly modified version of the example included in our documentation: https://docs.gravitypdf.com/v6/developers/php-form-data-array#nested-forms
Also, the data you provided shows there is not a child entry saved to field 226 so I cannot 100% confirm the above code would work and I’m only working from the information you have provided.
- This reply was modified 3 years ago by Jake Jackson.
Here is such a code in my template does not show anything. Field 22 is the first field in the child form
<tr> <td class="tg-e3zv">2. Застрахованное лицо<br></td> <td class="tg-031e" colspan="3" > <!-- The ID of our repeater field is 3 --> <?php $nested_ids = explode( ',', $form_data['field'][226] ); foreach ( $nested_ids as $id ) { $nested_form_data = GPDFAPI::get_form_data( $id ); if ( ! is_wp_error( $nested_form_data ) ) { /* * Output required content for this entry using the $nested_form_data array, * which has the same structure as $form_data (but for the current nested form entry) */ echo $nested_form_data['field'][22]; // output field ID 2 in the child form } } ?> </td> </tr>
And how to pull out separately, for example, the first and last name? Respectively id=22 and id=23 in the child form.
Stop! Everything worked! The data from the child form just didn’t register. Now they have appeared and you can pull everything out Thank you very much!
Now the question is how to pull data from a child form where there are several records – repeater. For example, 2 surnames, 2 first names, that is, data on two people…
Glad to hear you are able to load your Nested Forms data. As for displaying other fields in your Nested Form, it’s exactly as you would for the parent form, just under the variable name
$nested_form_data
i.e replace any references to$form_data
with$nested_form_data
from our documentation: https://docs.gravitypdf.com/v6/developers/php-form-data-array#accessing-common-field-dataIt is not clear how to output several lines line by line from the child form, where there is a first name (field 20) , last name (field 21) and email (field 22)?
This code does not output anything –<!-- The ID of our repeater field is 3 --> <?php foreach ( $form_data['repeater'][226] as $data ): ?> <div class="users"> <!-- The sub-field IDs will be unique to your repeater field --> <p>Name: <?= $data[20]?> <?= $data[21]?></p> <p>Email: <?= $data[22] ?></p> </div> <?php endforeach; ?>
- This reply was modified 3 years ago by Nitrat.
- The topic ‘Nested forms’ is closed to new replies.