9

I'm trying to submit an Entry programmatically from the front end. Everything works flawlessly, except, the asset fields never save. I've dug and dug through the Entries, Elements, and Content Services. Through similar Controllers; the Element Model. I can't find any errors, and no matter what I do, nothing seems to happen with Assets. Any ideas?

This is a simplified version of my html form:

<form method="post" action="" accept-charset="utf-8" enctype="multipart/form-data">
    <input type="hidden" name="action" value="plugin/save/saveEntry">

    <input type="hidden" name="title" value="Woo hoo">
    <input type="hidden" name="authorId" value="1">
    <input type="hidden" name="sectionId" value="10">
    <input type="hidden" name="locale" value="en_us">

    <input type="hidden" name="fields[matrixField]" value="">
    <input type="hidden" name="fields[matrixField][new1][type]" value="imageBlock">

    <h4>Upload Your Photo</h4>
    <input type="hidden" name="fields[matrixField][new1][fields][image]" value="">
    <input type="file" name="fields[matrixField][new1][fields][image][]" />

    <button>Submit</button>

</form>

And here's a simplified version of my Controller/Service (all jammed in the Controller for the convenience of sharing)

<?php
namespace Craft;

class Plugin_SaveController extends BaseController
{
    public function actionSaveEntry()
    {       
        $this->requirePostRequest();

        // Fields
        $entry = new EntryModel();
        $entry->sectionId  = 10;
        $entry->typeId     = 11;
        $entry->locale     = 'en_us';
        $entry->postDate   = date('Y-m-d');
        $entry->authorId   = 1;

        // Tell our content model where to look for custom fields
        $entry->setContentFromPost('fields');

        // Content
        $entry->getContent()->title = "Fancy Title";

        // Save the entry content
        craft()->entries->saveEntry($entry);

        $this->redirectToPostedUrl();

    }
}
6
  • This is more about something popping out at me versus me actually knowing what's wrong but, is this suppose to be here? Highlighted with asterisks <input type="file" name="fields[matrixField][new1][fields][image]*[]*" />
    – Natetronn
    Jun 28 '14 at 6:10
  • Looks right to me. Are you able to upload files to the field within the CP, by dragging-and-dropping files directly onto your Assets field within the Matrix field? Jun 28 '14 at 11:49
  • Is it a typo that the method is called actionSaveUser and the logic is saving an entry?
    – Brad Bell
    Jun 28 '14 at 14:39
  • 1
    I'm about 70% sure this is going to end up being a bug, but have pinged Brandon and Andris for a few clarifications.
    – Brad Bell
    Jun 28 '14 at 15:39
  • Yes, I can upload by dragging and dropping. I can also confirm, this works with a single assets field, it's just the Matrix that it doesn't work with. It's also not a typo with 'actionSaveUser', that's just a mistake in my simplification of the problem. The reason I need to do this is because we are trying to register a user and save a separate Entry for that same user in a single form. I'll update the example above so the typo doesn't exist here. But good catch! Jun 28 '14 at 16:03
4

This ended up being a Craft bug. We just released Craft 2.1 build 2562 with a fix.

1
  • I just updated to Craft Pro 2.1.2563 and can confirm the code posted above now works. Jul 8 '14 at 7:21

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