Version: current

# Class FileField

Represents a file type which can be added to a form. Automatically tries to save has_one-relations on the saved record.

Please set a validator on the form-object to get feedback about imposed filesize/extension restrictions.

CAUTION: Doesn't work in the CMS due to ajax submission, please use `FileIFrameField` instead.

  Object
  ViewableData implements IteratorAggregate
  RequestHandler
  FormField
  FileField

### Direct known subclasses

`FileIFrameField`, `MultiUploadField`, `SimpleImageField`

### Indirect known subclasses

`ImageField`

Package: forms\fields-files
Located at sapphire/forms/FileField.php

## Methods summary

`public` | # `__construct( string $name, string $title = null, integer $value = null, `Form` $form = null, string $rightTitle = null, string $folderName = null )` Create a new file field. Create a new file field. #### Parameters $name string $name The internal field name, passed to forms. $title string $title The field label. $value int $value The value of the field. $form Form $form Reference to the container form $rightTitle string $rightTitle Used in SmallFieldHolder() to force a right-aligned label $folderName string $folderName Folder to upload files to #### Overrides `FormField::__construct`
`public` | # `Field( )` Returns the form field - used by templates. Although FieldHolder is generally what is inserted into templates, all of the field holder templates make use of $Field. It's expected that FieldHolder will give you the "complete" representation of the field on the form, whereas Field will give you the core editing widget, such as an input tag. Returns the form field - used by templates. Although FieldHolder is generally what is inserted into templates, all of the field holder templates make use of $Field. It's expected that FieldHolder will give you the "complete" representation of the field on the form, whereas Field will give you the core editing widget, such as an input tag. Our base FormField class just returns a span containing the value. This should be overridden! #### Overrides `FormField::Field`
`public` | # `saveInto( `DataObject` $record )` Method to save this form field into the given data object. By default, makes use of $this->dataValue() Method to save this form field into the given data object. By default, makes use of $this->dataValue() #### Overrides `FormField::saveInto`
`public` | # `Value( )` Returns the field value - used by templates. Returns the field value - used by templates. #### Overrides `FormField::Value`
`public` | # `getValidator( )` Get custom validator for this field Get custom validator for this field
`public` | # `setValidator( object $validator )` Set custom validator for this field Set custom validator for this field #### Parameters $validator object $validator
`public integer` | # `getAllowedMaxFileSize( string $ext = null )` Get maximum file size for all or specified file extension. Falls back to the default filesize restriction ('*') if the extension was not found. Get maximum file size for all or specified file extension. Falls back to the default filesize restriction ('*') if the extension was not found. #### Deprecated 2.5 #### Parameters $ext string $ext #### Returns `integer` Filesize in bytes (0 means no filesize set)
`public` | # `setAllowedMaxFileSize( unknown_type $rules )` Set filesize maximums (in bytes). Automatically converts extensions to lowercase for easier matching. Set filesize maximums (in bytes). Automatically converts extensions to lowercase for easier matching. Example:
    array('*' => 200, 'jpg' => 1000)
#### Deprecated 2.5 #### Parameters $rules unknown_type $rules
`public array` | # `getAllowedExtensions( )` #### Deprecated 2.5 #### Returns `array` array
`public` | # `setAllowedExtensions( array $rules )` #### Deprecated 2.5 #### Parameters $rules array $rules
`public` | # `setFolderName( string $folderName )` #### Parameters $folderName string $folderName
`public string` | # `getFolderName( )` #### Returns `string` string
`public` | # `validate( mixed $validator )` Validation Functions for each field type by default formfield doesnt have a validation function Validation Functions for each field type by default formfield doesnt have a validation function #### Overrides `FormField::validate`
`public` | # `performReadonlyTransformation( )` Returns a readonly version of this field. Returns a readonly version of this field. #### Overrides `FormField::performReadonlyTransformation`

### Methods inherited from FormField

`FieldHolder()`, `LeftTitle()`, `Link()`, `Message()`, `MessageType()`, `Name()`, `Required()`, `RightTitle()`, `SmallFieldHolder()`, `Title()`, `Type()`, `addExtraAttribute()`, `addExtraClass()`, `allowHTML5()`, `allow_html5()`, `attrName()`, `attrTitle()`, `attrValue()`, `createTag()`, `dataValue()`, `debug()`, `describe()`, `extraClass()`, `fieldHolderTemplates()`, `fieldTemplates()`, `forTemplate()`, `getCustomValidationMessage()`, `getExtraClasses()`, `getForm()`, `getHTML5Attribute()`, `getTabIndex()`, `getTabIndexHTML()`, `hasClass()`, `hasData()`, `id()`, `isComposite()`, `isDisabled()`, `isReadonly()`, `jsValidation()`, `name_to_label()`, `performDisabledTransformation()`, `removeExtraAttribute()`, `removeExtraClass()`, `rootFieldSet()`, `securityTokenEnabled()`, `setAutocomplete()`, `setContainerFieldSet()`, `setCustomValidationMessage()`, `setDisabled()`, `setError()`, `setFieldHolderTemplate()`, `setForm()`, `setHTML5Attribute()`, `setLeftTitle()`, `setName()`, `setReadonly()`, `setRightTitle()`, `setSmallFieldHolderTemplate()`, `setTabIndex()`, `setTemplate()`, `setTitle()`, `setValue()`, `smallFieldHolderTemplates()`, `transform()`, `useHTML5()`, `use_html5()`

### Methods inherited from RequestHandler

`allowedActions()`, `checkAccessAction()`, `getRequest()`, `handleRequest()`, `hasAction()`, `httpError()`

### Methods inherited from ViewableData

`ATT_val()`, `BaseHref()`, `CSSClasses()`, `ColumnBreak()`, `ColumnCalc()`, `ColumnNumber()`, `ColumnPad()`, `ColumnPos()`, `CurrentMember()`, `CurrentPage()`, `Debug()`, `Even()`, `EvenOdd()`, `First()`, `FirstLast()`, `HasPerm()`, `IsAjax()`, `JS_val()`, `Last()`, `Me()`, `Middle()`, `MiddleString()`, `Modulus()`, `MultipleOf()`, `Odd()`, `Pos()`, `RAW_val()`, `SQL_val()`, `ThemeDir()`, `ThemeName()`, `Top()`, `TotalItems()`, `XML_val()`, `__get()`, `__isset()`, `__set()`, `buildCastingCache()`, `cachedCall()`, `castingClass()`, `castingHelper()`, `castingHelperPair()`, `castingObjectCreator()`, `castingObjectCreatorPair()`, `customise()`, `defineMethods()`, `escapeTypeForField()`, `getField()`, `getIterator()`, `getSecurityID()`, `getXMLValues()`, `hasField()`, `hasValue()`, `i18nLocale()`, `iteratorProperties()`, `obj()`, `renderWith()`, `setCustomisedObj()`, `setField()`

### Methods inherited from Object

`__call()`, `__toString()`, `__wakeup()`, `addMethodsFrom()`, `addStaticVars()`, `addWrapperMethod()`, `add_extension()`, `add_static_var()`, `allMethodNames()`, `cacheToFile()`, `cacheToFileWithArgs()`, `clearCache()`, `combined_static()`, `create()`, `createMethod()`, `create_from_string()`, `exists()`, `extInstance()`, `extend()`, `getCustomClass()`, `getExtensionInstance()`, `getExtensionInstances()`, `get_extensions()`, `get_static()`, `hasExtension()`, `hasMethod()`, `has_extension()`, `invokeWithExtensions()`, `is_a()`, `loadCache()`, `parentClass()`, `parse_class_spec()`, `remove_extension()`, `sanitiseCachename()`, `saveCache()`, `set_stat()`, `set_static()`, `set_uninherited()`, `stat()`, `strong_create()`, `uninherited()`, `uninherited_static()`, `useCustomClass()`

### Magic methods summary

## Properties summary

`public array` | $allowedMaxFileSize | # Restrict filesize for either all filetypes or a specific extension, with extension-name as array-key and the size-restriction in bytes as array-value. Restrict filesize for either all filetypes or a specific extension, with extension-name as array-key and the size-restriction in bytes as array-value. #### Deprecated 2.5
`public array` | $allowedExtensions | # Collection of extensions. Extension-names are treated case-insensitive. Example:
    array("jpg","GIF")
Collection of extensions. Extension-names are treated case-insensitive. Example:
    array("jpg","GIF")
`public boolean` | $relationAutoSetting | # Flag to automatically determine and save a has_one-relationship on the saved record (e.g. a "Player" has_one "PlayerImage" would trigger saving the ID of newly created file into "PlayerImageID" on the record). Flag to automatically determine and save a has_one-relationship on the saved record (e.g. a "Player" has_one "PlayerImage" would trigger saving the ID of newly created file into "PlayerImageID" on the record).
`protected `Upload`` | $upload | # Upload object (needed for validation and actually moving the temporary file created by PHP). Upload object (needed for validation and actually moving the temporary file created by PHP).
`protected string` | $folderName | # Partial filesystem path relative to /assets directory. Defaults to 'Uploads'. Partial filesystem path relative to /assets directory. Defaults to 'Uploads'.

### Properties inherited from FormField

`$autocomplete`, `$containerFieldSet`, `$customValidationMessage`, `$description`, `$disabled`, `$dontEscape`, `$extraAttributes`, `$extraClass`, `$extraClasses`, `$fieldHTML5`, `$fieldHolderTemplate`, `$form`, `$html5Attributes`, `$leftTitle`, `$message`, `$messageType`, `$name`, `$possible_autocomplete_values`, `$readonly`, `$rightTitle`, `$smallFieldHolderTemplate`, `$tabIndex`, `$template`, `$title`, `$use_html5`, `$value`

### Properties inherited from RequestHandler

`$allowed_actions`, `$brokenOnConstruct`, `$request`, `$url_handlers`

### Properties inherited from ViewableData

`$casting`, `$customisedObject`, `$default_cast`, `$failover`, `$iteratorPos`, `$iteratorTotalItems`

### Properties inherited from Object

`$class`, `$extension_instances`, `$extensions`

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -