<p>This ended up being a Craft bug. We just released <a href="http://buildwithcraft.com/updates#build2562" rel="nofollow">Craft 2.1 build 2562</a> with a fix.</p>

CC BY-SA 3.0
2014-07-08T07:21:39.603Z
I just updated to Craft Pro 2.1.2563 and can confirm the code posted above now works.
Ben Parizek
CC BY-SA 3.0
2014-07-01T22:33:37.617Z
2014-07-01T22:33:37.617Z
Brandon Kelly
<p>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?</p>

<p>This is a simplified version of my html form:</p>

<pre class="lang-html prettyprint-override"><code>&lt;form method="post" action="" accept-charset="utf-8" enctype="multipart/form-data"&gt;
    &lt;input type="hidden" name="action" value="plugin/save/saveEntry"&gt;

    &lt;input type="hidden" name="title" value="Woo hoo"&gt;
    &lt;input type="hidden" name="authorId" value="1"&gt;
    &lt;input type="hidden" name="sectionId" value="10"&gt;
    &lt;input type="hidden" name="locale" value="en_us"&gt;

    &lt;input type="hidden" name="fields[matrixField]" value=""&gt;
    &lt;input type="hidden" name="fields[matrixField][new1][type]" value="imageBlock"&gt;

    &lt;h4&gt;Upload Your Photo&lt;/h4&gt;
    &lt;input type="hidden" name="fields[matrixField][new1][fields][image]" value=""&gt;
    &lt;input type="file" name="fields[matrixField][new1][fields][image][]" /&gt;

    &lt;button&gt;Submit&lt;/button&gt;

&lt;/form&gt;
</code></pre>

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

<pre class="lang-php prettyprint-override"><code>&lt;?php
namespace Craft;

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

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

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

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

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

        $this-&gt;redirectToPostedUrl();

    }
}
</code></pre>

CC BY-SA 3.0
2014-06-28T06:10:36.137Z
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
CC BY-SA 3.0
2014-06-28T11:49:29.770Z
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?
Brandon Kelly
CC BY-SA 3.0
2014-06-28T14:39:27.333Z
Is it a typo that the method is called actionSaveUser and the logic is saving an entry?
Brad Bell
CC BY-SA 3.0
2014-06-28T15:39:17.353Z
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
CC BY-SA 3.0
2014-06-28T16:03:16.950Z
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!
Ben Parizek
CC BY-SA 3.0
2014-07-04T18:17:51.783Z
Just tested locally and latest build, Ben and it's working for me now.
Brad Bell
<plugin-development><assets><plugins>
How do I save a Matrix field with an asset block programmatically?
# How do I save a Matrix field with an asset block programmatically?

(Asked by: username_0 on 2014-06-28)

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:

    

    
    
    
    

    
    

    Upload Your Photo
    
    

    Submit



And here's a simplified version of my Controller/Service (all jammed in the Controller for the convenience of sharing)
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();

    }
}

**Tags:** plugin-development, assets, plugins
> **Comments:**
> **username_1**: 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]*[]*" />`  
> **username_2**: 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?  
> **username_3**: Is it a typo that the method is called actionSaveUser and the logic is saving an entry?  
> **username_3**: I'm about 70% sure this is going to end up being a bug, but have pinged Brandon and Andris for a few clarifications.  
> **username_0**: 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!  
> **username_3**: Just tested locally and latest build, Ben and it's working for me now.  

---

## 1 answers

### Answer by username_2 on 2014-07-01. Score: 4 | Accepted answer

This ended up being a Craft bug. We just released [Craft 2.1 build 2562](http://buildwithcraft.com/updates#build2562) with a fix.

> **Comments:**
> **username_0**: I just updated to Craft Pro 2.1.2563 and can confirm the code posted above now works.  

---

