Class SimpleImageField
SimpleImageField provides an easy way of uploading images to Image
has_one relationships. These relationships are auto-detected if you name the
field accordingly. Unlike ImageField, it doesn't use an iframe.
Restricts the upload size to 2MB by default, and only allows upload of files with the extension 'jpg', 'gif' or 'png'.
Usage
class Article extends DataObject { static $has_one = array('MyImage' => 'Image'); } // use in your form constructor etc. $myField = new SimpleImageField('MyImage');
Usage within a controller
First add your $has_one relationship:
static $has_one = array( 'FileName' => 'FileType' );
(i.e. Image for a FileType)
Then add your Field into your form:
function Form() { return new Form($this, "Form", new FieldSet( new SimpleImageField ( $name = "FileTypeID", $title = "Upload your FileType" ) ), new FieldSet( // List the action buttons here - doform executes the function 'doform' below new FormAction("doform", "Submit") // List the required fields here ), new RequiredFields( "FileTypeID" )); } // Then make sure that the file is saved into the assets area: function doform($data, $form) { $file = new File(); $file->loadUploaded($_FILES['FileTypeID']); // Redirect to a page thanking people for registering Director::redirect('thanks-for-your-submission/'); }
Your file should be now in the uploads directory
- Object
-
ViewableData
implements
IteratorAggregate
-
RequestHandler
-
FormField
-
FileField
-
SimpleImageField
Methods summary
public
|
#
__construct( string $name, string $title = null, integer $value = null,
Create a new file field. |
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. |
public
|
Methods inherited from FileField
Value(),
getAllowedExtensions(),
getAllowedMaxFileSize(),
getFolderName(),
getValidator(),
saveInto(),
setAllowedExtensions(),
setAllowedMaxFileSize(),
setFolderName(),
setValidator(),
validate()
Methods inherited from FormField
FieldHolder(),
LeftTitle(),
Link(),
Message(),
MessageType(),
Name(),
Required(),
RightTitle(),
SmallFieldHolder(),
Title(),
Type(),
addExtraClass(),
attrName(),
attrTitle(),
attrValue(),
createTag(),
dataValue(),
debug(),
describe(),
extraClass(),
forTemplate(),
getCustomValidationMessage(),
getForm(),
getTabIndex(),
getTabIndexHTML(),
hasClass(),
hasData(),
id(),
isComposite(),
isDisabled(),
isReadonly(),
jsValidation(),
name_to_label(),
performDisabledTransformation(),
removeExtraClass(),
rootFieldSet(),
securityTokenEnabled(),
setContainerFieldSet(),
setCustomValidationMessage(),
setDisabled(),
setError(),
setForm(),
setLeftTitle(),
setName(),
setReadonly(),
setRightTitle(),
setTabIndex(),
setTitle(),
setValue(),
transform()
Methods inherited from RequestHandler
allowedActions(),
checkAccessAction(),
getRequest(),
handleRequest(),
hasAction(),
httpError()
Methods inherited from ViewableData
ATT_val(),
BaseHref(),
CSSClasses(),
CurrentMember(),
CurrentPage(),
Debug(),
Even(),
EvenOdd(),
First(),
FirstLast(),
HasPerm(),
IsAjax(),
JS_val(),
Last(),
Me(),
Middle(),
MiddleString(),
Modulus(),
MultipleOf(),
Odd(),
Pos(),
RAW_val(),
SQL_val(),
ThemeDir(),
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(),
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
|
$allowedExtensions |
#
Collection of extensions. Extension-names are treated case-insensitive. Example: array("jpg","GIF") |
Properties inherited from FileField
$allowedMaxFileSize,
$folderName,
$relationAutoSetting,
$upload
Properties inherited from FormField
$containerFieldSet,
$customValidationMessage,
$description,
$disabled,
$dontEscape,
$extraClass,
$extraClasses,
$form,
$leftTitle,
$message,
$messageType,
$name,
$readonly,
$rightTitle,
$tabIndex,
$title,
$value
Properties inherited from RequestHandler
$allowed_actions,
$brokenOnConstruct,
$request,
$url_handlers
Properties inherited from ViewableData
$casting,
$customisedObject,
$default_cast,
$failover,
$iteratorPos,
$iteratorTotalItems
Comments
Use the Silverstripe Forum to ask questions.