Issue with Displaying textarea Fields in a Repeater Field

Hello,

I am encountering an issue with the implementation of a repeater field in HivePress. Although I have successfully added a repeater field (program_test) to my listing attributes, I am facing difficulties with the display of textarea fields within this repeater.

Here’s a brief overview of the problem:

  1. Field Configuration: I’ve added a repeater field (program_test) with an inner textarea field (jour) to the listing attributes using the hivepress/v1/models/listing/attributes filter.

  2. Successful Addition: My logs confirm that the program_test field is being successfully added. The structure of this field includes a textarea type for the jour field.

  3. Rendering Issue: Despite the successful addition of the field, the textarea component does not appear in the front-end. I’ve used standard HivePress methods to add and configure these fields.

  4. Error Messages: I receive PHP warnings about an “Undefined array key ‘type’” in the class-model.php file, suggesting an issue with the handling of the type attribute for fields.

  5. Attempts to Resolve: I’ve tried disabling other plugins to check for conflicts and ensured my HivePress version is up to date. I’ve also followed examples and discussions on the forum but haven’t managed to resolve the issue.

I am looking for guidance or suggestions on how to properly display the textarea fields within a repeater field in HivePress. Any assistance or insights from the community would be greatly appreciated.

`// Ajout des attributs de listing personnalisés, y compris un champ répétable.
add_filter('hivepress/v1/models/listing/attributes', function($attributes) {
    // Log avant l'ajout du champ.
    error_log('Attempting to add program_test field to listing attributes.');

    // Ajout du champ répétable 'program_test'.
    $attributes['program_test'] = [
        'type'    => 'repeater',
        'label'   => esc_html__('Programme Test', 'text-domain'),
        'fields'  => [
            'jour' => [
                'type'        => 'textarea',
                'label'       => esc_html__('Jour', 'text-domain'),
                'description' => esc_html__('Description du jour', 'text-domain'),
            ],
        ],
    ];

    // Log après l'ajout du champ.
    if (isset($attributes['program_test'])) {
        error_log('program_test field added successfully.');
        // Log de la structure du champ répétable pour vérification.
        error_log(print_r($attributes['program_test'], true));
    } else {
        error_log('Failed to add program_test field.');
    }

    return $attributes;
});
`

Thank you in advance for your help!

Hello,

I have successfully implemented a custom repeater field in a HivePress listing update form, where the field is now displaying correctly. However, I’ve encountered a new issue concerning the handling and saving of data within this field.

Here’s a brief overview of my setup:

  1. Form Submission Filter: I’ve added the repeater field to the listing submission form using the hivepress/v1/forms/listing_submit filter. The field structure includes a custom_text sub-field.
  2. Form Update Filter: Using the hivepress/v1/forms/listing_update filter, I fetch and deserialize the existing data for the repeater field from the post meta. While the data retrieval and field display are successful, there seems to be an issue with the way the data is handled or saved.
  3. Create and Update Actions: The hivepress/v1/models/listing/create and hivepress/v1/models/listing/update actions are used for saving the repeater field data. The data appears to be stored correctly as per my logs, but I’m facing challenges in ensuring that the updated data in the repeater field is correctly saved and persists across page reloads.

Here are some logs for reference:

plaintextCopy code

[Date & Time] Début de la configuration du formulaire de mise à jour de listing.
[Date & Time] Objet de mise à jour de listing trouvé.
[Date & Time] Objet listing récupéré avec succès.
[Date & Time] ID de l'annonce : [listing_id]
[Date & Time] Valeur désérialisée du champ répéteur : Array ( [unique_id] => Array ( [custom_text] => test_content ) )

The repeater field data seems to be handled correctly on the front end, but the updates don’t appear to be consistently saved or displayed after form submission and page refresh.

Could anyone provide insights into what might be causing this issue or suggest further debugging steps? Any help or guidance would be immensely helpful.

Thank you!

Sorry for the delay.

Please try adding the repeater field via the hivepress/v1/forms/listing_submit and hivepress/v1/models/listing filters (also add '_external' => true parameter to the field array), then HivePress will handle validation and storage automatically since the field will also be added to the listing model. You can have to add custom code to validate, serialize/deserialize etc. values.

Thank you for your previous assistance with the repeater field in HivePress.

I have followed your instructions to add the repeater field via the hivepress/v1/forms/listing_submit and hivepress/v1/models/listing filters with the _external parameter set to true. However, I am still encountering an issue: the repeater fields display the correct number of fields but do not populate with the existing data when updating a listing.

Here are the relevant logs after implementing your suggested solution:

lessCopy code

[03-Jan-2024 18:05:18 UTC] Début de la configuration du formulaire de mise à jour de listing.
[03-Jan-2024 18:05:18 UTC] Objet de mise à jour de listing trouvé.
[03-Jan-2024 18:05:18 UTC] Objet listing récupéré avec succès.
[03-Jan-2024 18:05:18 UTC] ID de l'annonce : 757
[03-Jan-2024 18:05:18 UTC] Valeur brute du champ répéteur récupérée : Array (...)
[03-Jan-2024 18:05:18 UTC] Le champ répéteur n'est pas sérialisé ou est déjà un tableau.

As you can see, the data for the repeater field is being retrieved and appears to be structured correctly. However, this data is not being displayed in the repeater fields on the listing update form. The fields appear but remain empty, despite the data being present in the logs.

I’m looking for further insights into what might be causing this issue or suggestions on additional debugging steps that I can take. Your guidance would be greatly appreciated as this functionality is crucial for our listings.

Hello Ihor,

Thank you for your previous response. I’ve implemented the solution you suggested, but I’m still facing issues with the repeater field in the listing update form. I would appreciate any further guidance or insights.

Here’s a detailed overview of my implementation and the issue:

  1. Form Submission Filter: I’ve added the repeater field to the listing submission form using the hivepress/v1/forms/listing_submit filter. The field includes a custom_text sub-field. Here’s the code snippet for this part:
add_filter('hivepress/v1/forms/listing_submit', function($form) {
    $form['fields']['programme_par_jour'] = [
        'label' => 'Programme par jour',
        'description' => 'Ajoutez vos éléments ici',
        'type' => 'repeater',
        '_order' => 1000,
        '_external' => true,
        'fields' => [
            'custom_text' => [
                'label' => 'Texte Personnalisé',
                'description' => 'Entrez votre texte ici',
                'type' => 'textarea',
                'required' => true,
                'min_length' => 10,
            ],
        ],
    ];

    return $form;
});
  1. Form Update Filter: Using the hivepress/v1/forms/listing_update filter, I fetch and deserialize the existing data for the repeater field from the post meta. The data retrieval and field display are successful, but there seems to be an issue with how the data is handled or saved. Here’s the relevant code:
add_filter('hivepress/v1/forms/listing_update', function($form, $listing_update_object) {
    if ($listing_update_object) {
        $listing_object = $listing_update_object->get_model();

        if ($listing_object) {
            $listing_id = $listing_object->get_id();
            $custom_repeater_field_value = get_post_meta($listing_id, 'programme_par_jour', true);

            // Désérialiser les valeurs si nécessaire
            if (!is_array($custom_repeater_field_value)) {
                $custom_repeater_field_value = maybe_unserialize($custom_repeater_field_value);
            }

            // Reconstruire les valeurs du champ répéteur
            if (is_array($custom_repeater_field_value)) {
                $reconstructed_values = array_map(function($item) {
                    return ['custom_text' => $item['custom_text']];
                }, $custom_repeater_field_value);
            } else {
                $reconstructed_values = [];
            }

            $form['fields']['custom_repeater_field'] = [
                'type' => 'repeater',
                'label' => esc_html__('Programme par jour', 'text-domain'),
                'description' => esc_html__('Ajoutez vos éléments ici', 'text-domain'),
                'value' => $reconstructed_values,
                '_order' => 1000,
                '_external' => true,
                'fields' => [
                    'custom_text' => [
                        'type' => 'textarea',
                        'label' => esc_html__('Texte Personnalisé', 'text-domain'),
                        'description' => esc_html__('Entrez votre texte ici', 'text-domain'),
                        'required' => true,
                        'min_length' => 10,
                    ],
                ],
            ];
        } else {
            error_log('Objet listing non trouvé dans l\'objet de mise à jour.');
        }
    } else {
        error_log('Objet de mise à jour de listing non trouvé.');
    }

    return $form;
}, 10, 2);
// Enregistrement des données lors de la création d'un listing
add_action('hivepress/v1/models/listing/create', function($listing_id) {
    if (isset($_POST['programme_par_jour'])) {
        // Log des données spécifiques du champ 'programme_par_jour'
        error_log('Données programme_par_jour lors de la création: ' . print_r($_POST['programme_par_jour'], true));

        update_post_meta($listing_id, 'programme_par_jour', $_POST['programme_par_jour']);
    }
});

// Enregistrement des données lors de la mise à jour d'un listing
add_action('hivepress/v1/models/listing/update', function($listing_id) {
    if (isset($_POST['programme_par_jour'])) {
        // Log des données spécifiques du champ 'programme_par_jour'
        error_log('Données programme_par_jour lors de la mise à jour: ' . print_r($_POST['programme_par_jour'], true));

        update_post_meta($listing_id, 'programme_par_jour', $_POST['programme_par_jour']);
    }
});

  1. Create and Update Actions: The hivepress/v1/models/listing/create and hivepress/v1/models/listing/update actions are used for saving the repeater field data. The data seems to be stored correctly according to my logs, but I’m facing challenges ensuring that the updated data in the repeater field is correctly saved and persists across page reloads.
  2. Logs for Reference:

sqlCopy code

[Date & Time] Start of the listing update form configuration.
[Date & Time] Listing update object found.
[Date & Time] Listing object successfully retrieved.
[Date & Time] Listing ID: [listing_id]
[Date & Time] Deserialized repeater field value: Array ( [unique_id] => Array ( [custom_text] => test_content ) )

The repeater field data seems to be handled correctly on the front end, but the updates don’t appear to be consistently saved or displayed after form submission and page refresh.

Could anyone provide insights into what might be causing this issue or suggest further debugging steps? Any help or guidance would be immensely helpful.

Additionally, I suspect the issue might be related to the dynamic IDs assigned to each repeater field. While the form correctly displays the number of repeater fields, it fails to insert the data into these fields. This discrepancy seems to arise because the IDs recorded in the logs are different from those in the update form fields. This mismatch of IDs might be causing the data not to bind correctly to the respective fields during the update process.

Here’s an example from the logs showing the dynamic IDs and their associated data:

csharpCopy code

[07-Jan-2024 13:50:20 UTC] Data for programme_par_jour during update: Array
(
    [659aabe83bf8c] => Array
        (
            [custom_text] => programme par jour 1
        )

    [tip7rh2lmcp] => Array
        (
            [custom_text] => programme par jour 2
        )
)

In contrast, the IDs in the update form’s repeater fields don’t match these, potentially leading to the data not being displayed. I am looking for a way to either synchronize these IDs or handle the repeater field values irrespective of their dynamic IDs.

Any suggestions on how to address this specific aspect, or general guidance on handling dynamic IDs in repeater fields within HivePress, would be greatly appreciated.

Thank you once again for your assistance!

Please remove all the code for serializing/deserializing values, all these are performed by HivePress if you added a field via the model filter hook. I realized there’s an easier way to do this, there’s the attributes API which allows you to add a new attribute (listing field with stored values) and also add it on back-end via a single hook. Please check some samples here Search · user:hivepress attributes · GitHub If you have any extension with a Repeater field you can var_dump attributes via this hook to check how this repeater field is defined.

Hope this helps.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.