Jump to:

23581 Posts in 19403 Topics by 2893 members

General Questions

SilverStripe Forums » General Questions » Has_One File, allowed file types/extensions?

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 3392 Views
  • jackson_gabbard
    Avatar
    Community Member
    13 Posts

    Has_One File, allowed file types/extensions? Link to this post

    So, I'm trying to setup a DataObject to have a file ( static $has_one = array ('Song'=>'File'); ). Because the file must be an Mp3 to work properly, I'm attempting to setup SS to force the user to upload an MP3. I have tried everything under the sun to make this happen with absolutely no luck.

    Here's what I've tried --

    Extending the File DataObject with an Mp3File descendant. On that class I set the static $allowed_file_types to an array with one value, 'mp3' -- the result: nada. Works just like an unmodified File dataobject.

    Using getCMSFields() to grab the FieldIFrameField and specify allowed types -- the result: no dice specifying anything about the file at all. The FileIFrameField seems to accept absolutely anything and everything within the size range permitted with no way to modify it.

    Using the FileDataObjectManager by adding a FileDataObjectManager in place of the FileIFrameField. The Result: Closer, but no apparent way to just populate a has_one field. Also, I got errors on processing the upload every time. Not sure what to make of it. I piddled with the HasOneFileDataObjectManager class, but couldn't get it working at all.

    Replacing the FileIFrameField with a FileField and setting that field to permit the proper filetype with setAllowedExtensions. The Result: No dice. Seems to have no effect at all.

    Fetching the trunk version of the FileIFrameField which is allgedly a fixed version. Result: No successful uploads at all. Allows me to select any file and when I attach the file, the iFrame refreshes to the form with no indication of success or failure.

    If anyone has solved this, I would love to know how you did it. Thanks!

  • jackson_gabbard
    Avatar
    Community Member
    13 Posts

    Re: Has_One File, allowed file types/extensions? Link to this post

    Okay, brief follow up. Doing some investigating, I'm finding that the allowedExtensions that I specify are not getting carried through to the save. I wonder if the problem is my approach to assigning them.

    Here's how I'm setting up the field:

    function getCMSFields ($params = null) {
       $fields = parent::getCMSFields($params);

       $song = new FileIFrameField('Song');
       $song->allowedExtensions = array ('mp3');
       $fields->replaceField('Song', $song);

       return $fields;
    }

    Prior to replacing the field, I attempted modifying the field in-place using $fields->fieldByName('Song')->allowed... still nothing.

    I'm determining that the allowedExtension assignments are not taking by cracking open the core FileField.php class (sapphire/forms/FileField.php) and putting in this

    die(print_r($this->allowedExtensions));

    on line 209 at the opening of the validate function. It dumps an empty array every time.

  • jackson_gabbard
    Avatar
    Community Member
    13 Posts

    Re: Has_One File, allowed file types/extensions? Link to this post

    Okay, so taking the long road on this one, but apparently it's a pretty well known issue that isn't slated for any sort of immediate bug fixing.

    http://open.silverstripe.com/ticket/2862

    Le sigh.

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: Has_One File, allowed file types/extensions? Link to this post

    Actually if you read the ticket comments you'll find it was already fixed in trunk a while back. It just hasn't made it into the 2.3 branch.

  • jackson_gabbard
    Avatar
    Community Member
    13 Posts

    Re: Has_One File, allowed file types/extensions? Link to this post

    Ah, I did read that actually. I just didn't say it right above. Duly noted and apologies for the misspeakin'.

  • timwjohn
    Avatar
    Community Member
    98 Posts

    Re: Has_One File, allowed file types/extensions? Link to this post

    Hi,

    What was the outcome of this? I'm having the same problem and in ModelAdmin and can't set the allowed filetypes for either a has_one image field or my own pdf file extension.

    I'm using 2.4-rc1. Just can't figure it out!

    Thanks.

  • timwjohn
    Avatar
    Community Member
    98 Posts

    Re: Has_One File, allowed file types/extensions? Link to this post

    http://silverstripe.org/customising-the-cms/show/282170?showPost=282489

    The thread talks about the now depreciated setAllowedExtensions method and how to get server side file validation.

    3392 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.