2

I have an assets field called "member_thumbnail" on my "members" channel (created by zoo visitor). When I make a channel:form edit form or the equivalent edit form from zoo visitor, I am able to successfully upload files using the native full assets field ({exp:assets_field_name}) but not with the simple file input method outlined at the docs here:

http://docs.pixelandtonic.com/assets/templating/channel_form.html

I found a workaround here: Simple HTML file field uploads with Assets and Safecracker

The workaround is to add a hidden input with NULL value for the field name you want to affect. Unfortunately, when you submit the form without changing the photo this way, the photo is cleared, naturally. So this workaround causes too many problems.

I want to use the simple method to avoid showing visitors all the uploaded files in the directory they are uploading to. Otherwise I would be happy to use the full assets field.

1 Answer 1

3

This is due to a change in how EE validates Channel Form fields (not sure when this was implemented). When you upload a file via a file field, that field name isn't actually populated in the $_POST array, so EE doesn't know it's been submitted, and hence won't call the fieldtype which owns that field.

It works around this issue for native File fields inside the Channel Form library like so:

if (in_array($field['field_type'], $this->file_fields))
{
    // trick validation into calling the file fieldtype
    if (isset($_FILES[$field['field_name']]['name']))
    {
        $_POST[$field['field_name']] = $_FILES[$field['field_name']]['name'];
    }
}

Thankfully there is a way to add additional fieldtypes to the file_fields array (which by default includes only File fieldtype). Just add this line to your config.php file:

$config['safecracker_file_fields'] = array('assets');

Voila. This method will now work for adding new uploads to an Assets field, or leaving the existing data alone without losing it on update:

<input type="file" name="my_file_field" />
<input type="hidden" name="my_file_field_filedir" value="1" />

UPDATE: Nothing's ever easy - there appears to be an EE bug which may still remove existing file selections. Reported here.

1
  • Wonderful, thank you. I have been bitten by isset vs. empty many times. Easy hack. Sep 4, 2015 at 12:17

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.