Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Has_One File, allowed file types/extensions?


Go to End


7 Posts   4839 Views

Avatar
jackson_gabbard

Community Member, 13 Posts

16 October 2009 at 8:13am

Edited: 16/10/2009 8:17am

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!

Avatar
jackson_gabbard

Community Member, 13 Posts

16 October 2009 at 8:58am

Edited: 16/10/2009 8:59am

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.

Avatar
jackson_gabbard

Community Member, 13 Posts

16 October 2009 at 10:01am

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.

Avatar
ajshort

Community Member, 244 Posts

16 October 2009 at 12:34pm

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.

Avatar
jackson_gabbard

Community Member, 13 Posts

16 October 2009 at 12:37pm

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

Avatar
timwjohn

Community Member, 98 Posts

9 April 2010 at 5:40am

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.

Avatar
timwjohn

Community Member, 98 Posts

10 April 2010 at 4:01am

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.