mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #7426 from silverstripe/filefield-accept-attr
NEW: Implement accept attribute in FileField (closes #7279)
This commit is contained in:
commit
d8f49a1bda
@ -2,10 +2,11 @@
|
|||||||
|
|
||||||
namespace SilverStripe\Forms;
|
namespace SilverStripe\Forms;
|
||||||
|
|
||||||
|
use SilverStripe\Assets\File;
|
||||||
|
use SilverStripe\Control\HTTP;
|
||||||
use SilverStripe\Core\Injector\Injector;
|
use SilverStripe\Core\Injector\Injector;
|
||||||
use SilverStripe\ORM\DataObject;
|
use SilverStripe\ORM\DataObject;
|
||||||
use SilverStripe\ORM\DataObjectInterface;
|
use SilverStripe\ORM\DataObjectInterface;
|
||||||
use SilverStripe\Assets\File;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a file type which can be added to a form.
|
* Represents a file type which can be added to a form.
|
||||||
@ -85,6 +86,43 @@ class FileField extends FormField implements FileHandleField
|
|||||||
return parent::Field($properties);
|
return parent::Field($properties);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getAttributes()
|
||||||
|
{
|
||||||
|
$attributes = parent::getAttributes();
|
||||||
|
|
||||||
|
$accept = $this->getAcceptFileTypes();
|
||||||
|
if ($accept) {
|
||||||
|
$attributes = array_merge(['accept' => implode(',', $accept)], $attributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $attributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of file extensions (and corresponding mime types) that will be accepted
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getAcceptFileTypes()
|
||||||
|
{
|
||||||
|
$extensions = $this->getValidator()->getAllowedExtensions();
|
||||||
|
if (!$extensions) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$accept = [];
|
||||||
|
$mimeTypes = HTTP::config()->uninherited('MimeTypes');
|
||||||
|
foreach ($extensions as $extension) {
|
||||||
|
$accept[] = ".{$extension}";
|
||||||
|
// Check for corresponding mime type
|
||||||
|
if (isset($mimeTypes[$extension])) {
|
||||||
|
$accept[] = $mimeTypes[$extension];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_unique($accept);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param DataObject|DataObjectInterface $record
|
* @param DataObject|DataObjectInterface $record
|
||||||
*/
|
*/
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace SilverStripe\Forms\Tests;
|
namespace SilverStripe\Forms\Tests;
|
||||||
|
|
||||||
|
use ReflectionMethod;
|
||||||
use SilverStripe\Dev\FunctionalTest;
|
use SilverStripe\Dev\FunctionalTest;
|
||||||
use SilverStripe\Control\Controller;
|
use SilverStripe\Control\Controller;
|
||||||
use SilverStripe\Forms\FileField;
|
use SilverStripe\Forms\FileField;
|
||||||
@ -11,7 +12,6 @@ use SilverStripe\Forms\RequiredFields;
|
|||||||
|
|
||||||
class FileFieldTest extends FunctionalTest
|
class FileFieldTest extends FunctionalTest
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test a valid upload of a required file in a form. Error is set to 0, as the upload went well
|
* Test a valid upload of a required file in a form. Error is set to 0, as the upload went well
|
||||||
*
|
*
|
||||||
@ -39,6 +39,24 @@ class FileFieldTest extends FunctionalTest
|
|||||||
$this->assertTrue($form->validationResult()->isValid());
|
$this->assertTrue($form->validationResult()->isValid());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @skipUpgrade
|
||||||
|
*/
|
||||||
|
public function testGetAcceptFileTypes()
|
||||||
|
{
|
||||||
|
$field = new FileField('image', 'Image');
|
||||||
|
$field->setAllowedExtensions('jpg', 'png');
|
||||||
|
|
||||||
|
$method = new ReflectionMethod($field, 'getAcceptFileTypes');
|
||||||
|
$method->setAccessible(true);
|
||||||
|
$allowed = $method->invoke($field);
|
||||||
|
|
||||||
|
$expected = ['.jpg', '.png', 'image/jpeg', 'image/png'];
|
||||||
|
foreach ($expected as $extensionOrMime) {
|
||||||
|
$this->assertContains($extensionOrMime, $allowed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test different scenarii for a failed upload : an error occured, no files where provided
|
* Test different scenarii for a failed upload : an error occured, no files where provided
|
||||||
* @skipUpgrade
|
* @skipUpgrade
|
||||||
|
Loading…
Reference in New Issue
Block a user