1

I am using the grid field as a custom field for one of my channels. My form is setup correctly from what I can tell. What happens is, when trying to add to an entry and already having added a few grid rows worth of data from the CP.

After I submit a new entry using Channel:Form it overrides all the other data and leaves me with 1 field only. I am wanting the grid field to just update and leave the other grid data alone in that channel entry, but it replaces all of my data with just the one newly submitted record. How do I go about simply adding onto the data already present in the Entry with my Grid field using Channel:Form?

This is what I am using in my form, any ideas?

<input type="text" name="field_id_10[rows][new_row_0][col_id_21]"> 
3
  • Vin, did the answers below help at all? It's good to mark an answer or add a comment if you're still stuck, it helps other site users find answers!
    – Blatant
    May 14, 2015 at 14:50
  • Tons sorry about not replying sooner, it's not perfect but that is a great solution, many thanks! Will mark it now :)
    – Vin
    May 20, 2015 at 11:43
  • Glad it got you to the solution, agreed it's not graceful or EEish, but it'll work :)
    – Blatant
    May 20, 2015 at 11:51

2 Answers 2

1

From this answer : How to work with the new channel forms and the new grid fieldtype ( and updateing it)

Is using the {field:field_name} variable not to your requirements? I tested this and it works a treat. Otherwsie if you want to insert manually you can do it but you'll need a little php to get the right col and row indexes...

To do it manually, or without revealing the grid I've an example below, you will need to add the existing values within hidden fields, as without them the existing data is wiped.

<?php
    //first we need the column ID's for the grid. In my example these happen to be 3 and 4, you can get these id's from the exp_grid_columns table in the DB, I hard code them here as I know then but for portability you could write a query to get these
    $col1 = 3;
    $col2 = 4;

    //next we need to know what the new row index is, we also need the existing values so I'll do a little query here to get it all...
    // my grid field ID is 5, label is 'children', this entry ID is 11
    $res = ee()->db->where('entry_id', '11')
                   ->order_by('row_order', 'ASC')
                   ->get('channel_grid_field_5');

    //so lets count and display our results if any, for the input names, first its the grid field label (children) then rows, then the row ID and then col ID
    $new_row = 1;
    if ( $res->num_rows() > 0 ) {
        //remember next row (starts at 1 not 0)
        $new_row = $res->num_rows() + 1;
        //echo out the hidden fields
        foreach ( $res->result_array() as $row ) {
            //First Column
            //create the input name separate for readability!
            $inputName = 'children[rows]';
            $inputName .= '[row_id_' . $row['row_id'] . ']';
            $inputName .= '[col_id_' . $col1 . ']';
            echo '<input type="hidden" name="' . $inputName . '" value="' . $row['col_id_' . $col1] . '" />';
            //Second Column
            $inputName = 'children[rows]';
            $inputName .= '[row_id_' . $row['row_id'] . ']';
            $inputName .= '[col_id_' . $col2 . ']';
            echo '<input type="hidden" name="' . $inputName . '" value="' . $row['col_id_' . $col2] . '" />';
        }
    }

    //lastly, lets output text boxes for the new row
    //First Column
    echo '<input type="text" name="children[rows][new_row_' . $new_row . '][col_id_' . $col1 . ']" value="" dir="ltr" field_content_type="all" maxlength="256" />';
    //Second Column
    echo '<input type="text" name="children[rows][new_row_' . $new_row . '][col_id_' . $col2 . ']" value="" dir="ltr" field_content_type="all" maxlength="256" />';
?>

There you go!, this works well on my sandbox EE install, in my test I had 2 rows populated in CP, so I ended up with 4 hidden fields, and 2 new value text boxes each with new_row_3 in the name. Worked fine to append new rows to the grid.

Please note, my test was using the nice basic Text Input fields, other fiedl types might require different fashioning of their input names, and if one of the fields is a relationship field you're right in it! this wiuld constitute another question though, the example above shows how to add grid rows, without displaying the existing rows (if you want the entire matrix as it is in CP then see the answer linked at the beginning of this post).

Let us know how you get on!

PS> Sorry for the late reply, its been hectic here!

3
  • I so appreciate the answer, it's above and beyond what I expected. I do manually build my fields and it goes fine. Now the issue is truly when going back to update that entry using channel:form, it clears the entries and replaces the data, that is the true issue. Yes it all works fine and good from posting new, post 5 fields and I get 5 grid entries but if I go back to update from the frontend for that same entry id, those 5 get wiped and replaced with what I am trying to add to the fields.
    – Vin
    Apr 29, 2015 at 18:43
  • I hope that makes sense. I am updating but needing the fields to be added on, matrix does this but in all my testing Grid fails.
    – Vin
    Apr 29, 2015 at 18:43
  • Hi Vin, thats what the example above spells out. I tested this with an existing entry (I created an entry in CP and set 2 of the grid rows), I then used the code above to add the 3rd row to the grid (and a 4th!). I checked in CP and the data is not wiped if you get the names of the hidden fields correct. Its the hidden fields you need, when I tried it without the hidden "existing" fields it behaved as you said, wiping the existing data... If it helps edit your OP, add the full form template and tell us the grid field and column names and we'll step through it :)
    – Blatant
    Apr 30, 2015 at 9:22
0

If I recall correctly, you just need to number the 'new_row' correctly, lets say you have 2 rows already, when you submit the form it should be field_id_10[rows][new_row_2] (numbering starts from 0 obviously).

Also, I'm not sure if you have to resubmit the existing rows as well, like it does on the CP. you can easily confirm this by echoing out the existing days in hidden inputs.

My memory isn't what it was and is been a while since I did matrix/grid via channel entry form so I'll try to test this in the morning (UK) for you and solidify my answer, but in the meantime this will get you started.

1
  • Thanks for looking into this with me, so appreciate your help. I did setup a channel and custom fields to test this based on your suggestion. I still get the same results. This works in Matrix, I tested it but I really want to use Grid, not sure how much longer Matrix will be around now that Grid is competing.
    – Vin
    Apr 26, 2015 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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