0

I'm having issues saving an Asset field in my FEEF using a Plugin. I have a channel with some fields added to it, one of these fields being an Asset field. When using the standard entries/saveEntry build in function in Craft the Asset gets saved.

When I try to run the same form (same channel, same field) through my Plugin everything saves except the Asset field asset. The code I'm using (below) is a slightly changed version of Pixel&Tonics own "Guest Entries" Plugin.

Can anyone help me to figure out whats going on?

/**
 * Populates an EntryModel with post data.
 *
 * @access private
 * @throws HttpException
 * @return EntryModel
 */
private function _populateEntryModel()
{
    $entry = new EntryModel();

    // Set section
    $entry->sectionId   = craft()->request->getRequiredPost('sectionId');
    $this->_section     = craft()->sections->getSectionById($entry->sectionId);
    $this->_entryTypeId = craft()->request->getPost('entryTypeId');

    // Default
    $entry->authorId    = 1;
    $entry->enabled     = true;

    // Set title
    $entry->getContent()->title = craft()->request->getPost('title');

    // Set Entry type
    if($this->_entryTypeId)
    {
        $entry->typeId = $this->_entryTypeId;
    }

    // Add dynamic fields
    $entry->setContentFromPost(craft()->request->getParam('fields'));

    // Return model
    return $entry;
}

This is how my form looks (I've removed some styling and keept the essentials)

<form method="post" action="" accept-charset="UTF-8" enctype="multipart/form-data">

<!-- Hidden -->
{{ getCsrfInput() }}
<input type="hidden" name="action" value="forms/saveEntry">
<input type="hidden" name="redirect" value="{{ craft.request.url }}?sent=1">
<input type="hidden" name="sectionId" value="13">
<input type="hidden" name="entryTypeId" value="18">
<input type="hidden" name="title" class="title" value=""/>

<input type="text" name="fields[formName]" value="{% if formEntry.formName is defined %}{{ formEntry.formName }}{% endif %}" class="default-field name" />
<input type="text" name="fields[formConfirmationNumber]" value="{% if formEntry.formConfirmationNumber is defined %}{{ formEntry.formConfirmationNumber }}{% endif %}" class="default-field" />
<input type="text" name="fields[formRoom]" value="{% if formEntry.formRoom is defined %}{{ formEntry.formRoom }}{% endif %}" class="default-field" />
<input type="file" name="fields[formFile]">

</form>

The name="title" field gets set dynamic...

  • What does your front-end form look like? – Brad Bell Jan 14 '16 at 20:00
  • Added it to the original post @BradBell. – naboovalley Jan 16 '16 at 6:59
  • Also the site uses Craft Pro 2.4.2702. Don't know if that makes a difference. – naboovalley Jan 16 '16 at 7:00
  • Hrm... before I setup a test case, any chance you can update to the latest 2.5 to see if it helps? Lots of enhancement and bug fixes in it. – Brad Bell Jan 17 '16 at 0:20
  • Since its a quite large site I would like to troubleshoot it as is before running an update. I'm wondering if it has anything to do with me setting the "Entry Type" ID? Because when running it through entries/saveEntry I need to have the targeted "Entry Type" as the root one in the list (it needs to be sorted as default/top entry type in list) since entries/saveEntry doesn't support multiple entry types (I think?). Anything around this we can dig around in @BradBell? – naboovalley Jan 17 '16 at 9:32
1

Here's the issue:

// Add dynamic fields
$entry->setContentFromPost(craft()->request->getParam('fields'));

setContentFromPost requires that if you pass any uploaded files in, you need to pass the field name as a string, instead of the actual posted values.

Changing it to this and it should work:

$entry->setContentFromPost('fields');
| improve this answer | |

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.