添加类型元素的自定义选项

时间:2016-05-04 14:10:08

标签: symfony

我有表格,有两个这样的方法

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Justart\InvoiceBundle\Entity\InvoiceElement',
        'validation_groups' => 'invoiceElementValidation',
        'translation_domain' => 'JustartInvoiceBundle',
        'invoice_revision' => false,
    ));
}

public function buildView(FormView $view, FormInterface $form, array $options)
{
    if (!empty($options['invoice_revision'])) {
        $view->vars['invoice_revision'] = $options['invoice_revision'];
    }
}

表单名称为InvoiceElementType

我有一个名为InvoiceType的表单,其中包含我写的元素:

$builder->add('invoice_elements', CollectionType::class, array(
    'entry_type' => InvoiceElementType::class,
    'allow_add' => true,
    'by_reference' => false,
    'allow_delete' => true,
    'label' => false,
    'entry_options'  => array(
        'label' => false,
    ),
    'required' => true,
    'constraints' => array(
        new NotBlank(array('groups' => array('STANDARD','PROFORM','FINISH','ADVANCE'))),
    ),
    'invoice_revision' => $this->isRevision,
))

当我运行它时我得到错误

  

选项" invoice_revision"不存在。定义的选项是:   "动作" ...

那么我做错了什么?

如何从控制器向变量发送变量?我试过了:

$form = $this->createForm(new Form\InvoiceType(1), $invoice);

InvoiceType的构造需要整数值,但我得到的错误是createForm除了第一个参数

中的字符串

1 个答案:

答案 0 :(得分:0)

尝试在invoice_revision键下添加entry_options选项:

'entry_options'  => array(
    'label' => false,
    'invoice_revision' => $this->isRevision,
),