2

How must I populate a playa hidden input field (in a safecracker form) with multiple entries? I tried passing the entry IDs separated by comma, semicolon, bar, but nothing seems to work. Only the first entry in the array gets saved. How do I have to format the entry IDs for multiple relationships to be saved?

Update: Thanks to Jeremy I now know how to add data to Playa correctly. Alas, it's still not working, now I get PHP errors.

This is what I have (inside a safecracker form): {exp:channel:entries channel="channel-name" category="{category_id}" dynamic="no" disable="member_data|pagination"}

  • {title}{if {embed:parent_id} == '{exp:playa:parent_ids}' } {if:else} {/if}
  • {/exp:channel:entries}

    I make a list with checkboxes. The conditionals check whether this entry is related to the entry currently being edited. So when it's related

    <input class="relate" type="hidden" name="playa_field[selections][]" value="{entry_id}"/>
    

    gets displayed, when it's not related

    <input class="relate" type="hidden" name="0" value="{entry_id}"/>{/if}</label>
    

    gets displayed. This works fine.

    In the footer I load jQuery and this code to look for changes on checkboxes. When a checkbox is changed the "name" attribute gets updated.

        $(document).ready(function() {
            var $checkboxes = jQuery("#tag_list").find(":checkbox");
            $checkboxes.on('change', function () {
                var $relate = jQuery(this).siblings('.relate').attr("name"),
                $name = "playa_field[selections][]";
                if ($relate == $name) {
                    jQuery(this).siblings('.relate').attr( "name", "0" );
                }
                if ($relate != $name) {
                    jQuery(this).siblings('.relate').attr( "name", "playa_field[selections][]" );
                }
            });
        });
    

    This also seems to work, I can see the "name" attribute get changed in the Safari developer tools.

    But now, when I submit the field I get a bunch of the following PHP errors:

    Severity: Warning
    
    Message: array_filter() expects parameter 1 to be array, string given
    
    Filename: playa/ft.playa.php
    
    Line Number: 2356
    
    Severity: Warning
    
    Message: array_merge() [function.array-merge]: Argument #1 is not an array
    
    Filename: playa/ft.playa.php
    
    Line Number: 2356
    

    What am I doing wrong?

    2

    You'll want to actually use multiple hidden fields, each adding to an array:

    <input type="hidden" name="cf_playa_field[selections][]" value="123">
    <input type="hidden" name="cf_playa_field[selections][]" value="456">
    

    You could also potentially write an extension to rework the POST data if you absolutely must submit the values in one field.

    | improve this answer | |
    • Thank you. Do all of these hidden input fields need to be next to each other? Because I don't have them one after another and am getting PHP errors: "Severity: Warning Message: array_filter() expects parameter 1 to be array, string given Filename: playa/ft.playa.php Line Number: 2356" – Florian Sep 23 '13 at 19:45
    • It should work from anywhere in the form. Can you update the post with the relevant portion of your template? – Jeremy Gimbel Sep 24 '13 at 2:36
    • OK, I added template code. – Florian Sep 24 '13 at 5:59
    1

    OK, I got this squared away. It seems like my name="0" inputs were causing the errors. Now I remove the complete hidden input when a checkbox is unchecked and create a new one on the fly when a checkbox is checked.

    | improve this answer | |
    • Glad you got it working! – Jeremy Gimbel Sep 24 '13 at 14:36

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

    Not the answer you're looking for? Browse other questions tagged or ask your own question.