3

I've been experimenting with ExpressionEngine 2.7.0, attempting to replace my use of Matrix with Grid and updating my Safecracker forms with Channel Forms.

Currently, I have a template where users are able to create a channel entry in a SafeCracker form, which includes populating the first row in a Matrix field within that entry.

I'd like to replace that with a template where users are able to create an entry using a Channel Form and populate a row in a Grid field. I've successfully changed it to a Channel Form, but can't for the life of me figure out how to populate a Grid field. I'll share the code I was previously using:

        {exp:safecracker channel="routine" return="" json="yes" rules:field_id_3[row_new_0][col_id_1]="valid_ee_date"}
            <input name="RET" type="hidden" value="/" />
            <input name="title" type="hidden" value="New Routine" />
            <input name="routine_catalyst" type="text" />
            <input name="routine_reaction" type="text" />

            <input type="hidden" name="field_id_3[row_order][]" value="row_new_0" />
            <input type="hidden" name="field_id_3[row_new_0][col_id_1]" value="{entry_date}" />
            <select style="display: none" name="field_id_3[row_new_0][col_id_2]">
                <option value="n" selected="">NO</option>
                <option value="y">YES</option>
            </select>

        <select name="category[]" id="routine_category" size="4" multiple="multiple" style="display: none;">

            {categories}
           <option value="{category_id}"{selected}>{category_name}</option>
        {/categories}   

        </select>

        {/exp:safecracker}

and what I currently have below

    {exp:channel:form
        channel="routine"
        return=""
        json="yes"
    }

        <input name="RET" type="hidden" value="/" />
        <input name="title" type="hidden" value="New Routine" />
        <input name="routine_catalyst" type="text" />
        <input name="routine_reaction" type="text" />

        <input type="text" name="routine_occurence[row_order][]" value="row_new_0" />

        <input type="" name="routine_occurence:date" value="{entry_date}" />
        <select style="" name="routine_occurence:complete">
            <option value="false" selected="">false</option>
            <option value="true">true</option>
        </select>

        <select name="category[]" id="routine_category" size="4" multiple="multiple">

        {categories}
            <option value="{category_id}"{selected}>{category_name}</option>
        {/categories}   

        </select>

        <input type="submit" value="submit" />

    {/exp:channel:form}

I know it's still new, but I was wondering whether anyone had had any success with this? The docs are pretty lacking in the area at the moment.

EDIT: I should add that when I use the above code, I receive PHP errors with the message:

Severity: Notice

Message: Undefined offset: 0

Filename: libraries/Grid_lib.php

Line Number: 238

And the entry is created, without the Grid row being entered

EDIT2: Solution below

  • I am also facing issue in Grid fields with channel forms. If we give static field id then it is not a good practice. Can anybody suggest any solution for this? – Abhilekha Dec 26 '13 at 11:36
3

So, I've found a way to achieve this, which seems to work in the reduced test case that I created (Grid fieldtype with one column of text type and only one entry being entered in at a time). In the future, I'll create test that's a bit more complex and see if it becomes more involved. The code for this is:

        <input type="hidden" name="field_id_9[rows][new_row_0][]" value="new_row_0" />
        <input type="text" name="field_id_9[rows][new_row_0][col_id_5]" value="{entry_date}" />

The magic numbers you see floating in there (9 & 5) correspond to the custom field ID for the Grid field and the column ID for the data respectively.

Strangely, the Column ID does not necessarily begin at 0. That is, I had three Grid fields in that Channel Field Group, the former two having two columns in them. This third Grid field had one column and was created last, making it column 5, despite the Grid fields not actually being one field.

So, this can be extended to add more than one row by creating further instances (new_row_1, new_row_2) etc, or extended to use more than one column by creating more inputs (col_id_6, col_id_6).

Hope this helps others and cheers for anyone who scratched their head for it.

| improve this answer | |
0

I've not tried this yet, but certainly [row_order] is not going to work because that's Matrix lingo. Any luck with using [row_id] instead?

| improve this answer | |
  • Cheers for the pointer. I gave it a go, but to no avail. Solution is above :) – David Leitch Sep 2 '13 at 13:06
  • cool. glad you got it and thanks for sharing. – Stephen Callender Sep 2 '13 at 18:08

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.