vria / enhanced-file

Symfony表单的增强文件类型

安装: 261

依赖: 0

建议者: 0

安全: 0

星标: 2

关注者: 2

分支: 1

类型:symfony-bundle

1.0.2 2016-05-08 21:14 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:36:38 UTC


README

Build Status

具有额外功能的Symfony表单文件类型

  • 如果文件之前已上传,则显示下载链接
  • 如果上传新文件,则可以删除之前上传的文件

##安装

使用 Composer,运行

composer require vria/enhanced-file

将VRiaNoDiacriticBundle添加到您的应用程序内核

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new VRia\Bundle\EnhancedFileBundle\VRiaEnhancedFileBundle(),
    );
}

##使用

在Symfony 3中,您应使用

$form = $this->createFormBuilder()
    ->add('file', EnhancedFileType::class, $options)
    ...

而在Symfony ~2.3中

$form = $this->createFormBuilder()
    ->add('file', 'enhanced_file', $options)
    ...

$options 是一个数组,包含增强的表单字段选项

  • directory_path - 放置文件的物理目录。例如 $this->get('kernel')->getRootDir() . '/../web/upload/'必需
  • public_directory_path - 从您的公共目录(通常是 /web)到文件目录的路径。例如 '/upload/'必需
  • delete_previous_file - 是否删除之前上传的文件。默认值为 true

因此,完整的定义可以是

$form = $this->createFormBuilder()
    ->add('file', EnhancedFileType::class, array(
        'label' => 'Curriculum vitae',
        'directory_path' => $this->get('kernel')->getRootDir() . '/../web/upload/',
        'public_directory_path' => '/upload/',
        'required' => false
    ))