I need to add .svg file extension to my configuration.
At the moment in my project I have other extensions as (pdf, images)
I made the following changes
- Added a new svg_file to the context
- Added the file provider (In the end of configuration file)
- Added the svg in allowed_extensions
- Added the image/svg+xml in allowed_mime_types
Now, I can upload the svg file but the problem is that the user can upload other file extensions for example pdf etc..
How can avoid it? Or find a proper way for the form validation?
The Sonata documentations:
helped me, but not for the form validation.
What I'm missing?
I changed the follows files:
#app/config/sonata_config.yml
sonata_media:
default_context: images_file
db_driver: doctrine_orm # or doctrine_mongodb, doctrine_phpcr
contexts:
pdf_file:
providers:
- sonata.media.provider.file
formats: ~
images_file:
providers:
- sonata.media.provider.image
formats:
1x: { width: 870 , height: 412 , quality: 80 }
2x: { width: 1740 , height: 824 , quality: 50 }
svg_file:
providers:
- sonata.media.provider.file
formats: ~
cdn:
server:
path: /uploads/media # http://media.sonata-project.org/
filesystem:
local:
directory: %kernel.root_dir%/../web/uploads/media
create: false
providers:
file:
service: sonata.media.provider.file
resizer: false
filesystem: sonata.media.filesystem.local
cdn: sonata.media.cdn.server
generator: sonata.media.generator.default
thumbnail: sonata.media.thumbnail.format
allowed_extensions: ['pdf', 'txt', 'rtf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pttx', 'odt', 'odg', 'odp', 'ods', 'odc', 'odf', 'odb', 'csv', 'xml','svg']
allowed_mime_types: ['application/pdf', 'application/x-pdf', 'application/rtf', 'text/html', 'text/rtf', 'text/plain', 'image/svg+xml']
Form File:
use Sonata\AdminBundle\Admin\Admin;
class CustomAdmin extends Admin
{
/**
* @param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add(
'NormalLogo',
'sonata_type_model_list',
array('required' => false),
array(
'link_parameters' => array('context' => 'images_file', 'provider' => 'sonata.media.provider.image'),
)
)
->add(
'SvgLogo',
'sonata_type_model_list',
array('required' => false),
array(
'link_parameters' => array('context' => 'svg_file', 'provider' => 'sonata.media.provider.file'),
)
)
->add('overriddenBy', 'sonata_type_model',
array(
'empty_value' => 'Not overridden',
'btn_add' => false,
'btn_list' => false,
'btn_delete' => false,
'btn_catalogue' => false,
)
);
}
}