symfony表单元素选项

时间:2013-01-10 02:47:25

标签: php forms file-upload symfony

好的,这有点令人费解,所以请耐心等待。我会尽量保持清晰简洁。

我正在使用Symfony2非常好的表单构建器系统来实现简单的crud,但是基本的文件上传元素并不能完全满足我的需求,所以我想通过添加缩略图预览和其他一些细节来扩展它。

读到这个,我发现我可以创建自己的自定义模板块来渲染文件元素:

http://symfony.com/doc/master/cookbook/form/form_customization.html#twig

这真的很酷,而且似乎工作得很好。但是,我将文件上传的路径存储在实体的单独属性中,基于此:

http://symfony.com/doc/master/cookbook/doctrine/file_uploads.html

因此,我需要一些方法让模板访问路径字段。我创建了一个自定义的FileType类:

<?php

namespace TechPeople\InvoiceBundle\Component\Form\Type;

use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;

use Symfony\Component\Form\Extension\Core\Type\FileType as SymfonyFileType;

class FileType extends SymfonyFileType {
    /**
     * {@inheritdoc}
     */
    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        $view->vars = array_replace($view->vars, array(
            'type'  => 'file',
            'value' => '',
            'path' => $options['path'],
        ));
    }
}

然后我将文件路径传递给表单构建器,如下所示:

<?php

namespace TechPeople\InvoiceBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class InvoiceType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        echo 'test';
        $builder
            ->add('month')
            ->add('year')
            ->add('expenses')
            ->add('due')
            ->add('paid')
            ->add('created')
            ->add('expense_amount', 'money')
            ->add('total_amount', 'money')
            ->add('attachment', 'file', array('path'=>$options['data']->getAttachmentPath()))
            ->add('vendor')
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'TechPeople\InvoiceBundle\Entity\Invoice'
        ));
    }

    public function getName()
    {
        return 'techpeople_invoicebundle_invoicetype';
    }
}

嗯,这看起来非常时髦,但是我在加载表单时遇到了这个错误:

The option "path" does not exist. Known options are: "attr", "block_name", "by_reference", "cascade_validation", "compound", "constraints", "csrf_field_name", "csrf_protection", "csrf_provider", "data", "data_class", "disabled", "empty_data", "error_bubbling", "error_mapping", "extra_fields_message", "intention", "invalid_message", "invalid_message_parameters", "label", "label_attr", "mapped", "max_length", "pattern", "post_max_size_message", "property_path", "read_only", "required", "translation_domain", "trim", "validation_constraint", "validation_groups", "virtual"
500 Internal Server Error - InvalidOptionsException

所以,看起来某个地方有一个允许的选项列表,但我找不到它。另外,我不是100%确定InvoiceType中的add()方法将options数组传递给FileType中的buildView()方法。我无法在这两件事之间跟踪代码。

1 个答案:

答案 0 :(得分:2)

首先,一旦创建了自定义类,就应该声明它(注册它)以用作file类型: http://symfony.com/doc/current/reference/dic_tags.html#form-type

<?php

namespace TechPeople\InvoiceBundle\Component\Form\Type;

use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;

use Symfony\Component\Form\Extension\Core\Type\FileType as SymfonyFileType;

class FileType extends SymfonyFileType 
{
/**
 * {@inheritdoc}
 */
public function buildView(FormView $view, FormInterface $form, array $options)
{
    $view->vars = array_replace($view->vars, array(
        'type'  => 'file',
        'value' => '',
        'path' => $options['path'],
    ));
}

/**
 * {@inheritdoc}
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'path' => null,
    ));
}

/**
 * {@inheritdoc}
 * 
 * POTENTIALLY declare it as child of file type.
 */
public function getParent()
{
    return 'file';
}
}