【发布时间】:2015-04-02 08:57:00
【问题描述】:
1 我想要做的是在我的表单小部件模板中添加自定义(在这种情况下选项是“角度”)选项:
{%- block widget_attributes -%}
id="{{ id }}" name="{{ full_name }}"
{%- if angular %} ng-model="{{ full_name }}"{% endif -%}
....
{%- if intention %} {{ intention }}{% endif -%}
{%- if read_only %} readonly="readonly"{% endif -%}
.....
{%- endblock widget_attributes -%}
我想决定我的 CustomFormType 中是否有表单选项。 但我无法实现。我尝试了不同的方法。
是否可以在主窗体中添加自定义选项?
我知道有很多教程展示了如何在子元素中传递自定义选项,例如http://symfony.com/doc/current/cookbook/form/create_form_type_extension.html
我研究了表单组件的核心,并且有类
namespace Symfony\Component\Form\Extension\Core\Type;
class FormType extends BaseType{}
有方法构建视图
public function buildView(FormView $view, FormInterface $form, array $options)
{
.....
$view->vars = array_replace($view->vars, array(
'read_only' => $readOnly,
'errors' => $form->getErrors(),
'valid' => $form->isSubmitted() ? $form->isValid() : true,
'value' => $form->getViewData(),
'data' => $form->getNormData(),
'required' => $form->isRequired(),
'max_length' => isset($options['attr']['maxlength']) ? $options['attr']['maxlength'] : null, // Deprecated
'pattern' => isset($options['attr']['pattern']) ? $options['attr']['pattern'] : null, // Deprecated
'size' => null,
'label_attr' => $options['label_attr'],
'compound' => $form->getConfig()->getCompound(),
'method' => $form->getConfig()->getMethod(),
'action' => $form->getConfig()->getAction(),
'submitted' => $form->isSubmitted(),
));
}
在 symfony 之上定义基本选项。我可以在表单模板中全局访问这些选项,但我找不到添加自己的方法。
【问题讨论】:
标签: symfony symfony-forms