Php Symfony 2表单生成器自定义选项在twig中提供
我想这样做:Php Symfony 2表单生成器自定义选项在twig中提供,php,forms,symfony,twig,Php,Forms,Symfony,Twig,我想这样做: $builder->add('firstname', 'text', array( 'myCustomOption' => 'optionValue' )); {% block form_row -%} {{ myCustomOption }} {%- endblock form_row %} class MyTextType extends AbstractType { pu
$builder->add('firstname', 'text', array(
'myCustomOption' => 'optionValue'
));
{% block form_row -%}
{{ myCustomOption }}
{%- endblock form_row %}
class MyTextType extends AbstractType
{
public function getParent()
{
return 'text';
}
public function getName()
{
return 'my_text';
}
....
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver
->setDefaults([
'myOption' => null
])
;
}
....
}
因此,我可以在myForm.html.twig中使用myCustomOption
,如下所示:
$builder->add('firstname', 'text', array(
'myCustomOption' => 'optionValue'
));
{% block form_row -%}
{{ myCustomOption }}
{%- endblock form_row %}
class MyTextType extends AbstractType
{
public function getParent()
{
return 'text';
}
public function getName()
{
return 'my_text';
}
....
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver
->setDefaults([
'myOption' => null
])
;
}
....
}
但在这种情况下,我得到:
“myCustomOption”选项不存在。已知选项为:[……]
试着这样做:
$builder->add('firstname', 'text', array(
'myCustomOption' => 'optionValue'
));
{% block form_row -%}
{{ myCustomOption }}
{%- endblock form_row %}
class MyTextType extends AbstractType
{
public function getParent()
{
return 'text';
}
public function getName()
{
return 'my_text';
}
....
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver
->setDefaults([
'myOption' => null
])
;
}
....
}
服务定义:
acme_demo_bundle.text_type:
class: Acme\DemoBundle\Form\Type\MyTextType
tags:
- { name: form.type, alias: my_text }
用法:
$builder->add('someName', 'my_text', ['myOption' => $value])
答案是:类型扩展 它允许扩展所需的表单类型
email/phone/checkbox
或grouptext/entity/choice
,甚至扩展所有可用的表单
。
(请记住,一个元素可以包含几种类型。例如,输入<代码>电子邮件的类型将为<代码>表单、文本、电子邮件)
只需创建扩展类:
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class TextTypeExtension extends AbstractTypeExtension {
public function getExtendedType() {
return 'form'; // What type should be extended
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
// Add optional option - you can also add required options
// and available values of this option
$resolver->setOptional(array('myOption'));
}
public function buildView(FormView $view, FormInterface $form, array $options) {
// You can put any logic here
// For example 'myOption' => 'big' can be transformed to
// 'myOption' => '300px'
if (array_key_exists('myOption', $options)) {
// Add your option to twig template
$view->vars['myOption'] = $options['myOption'];
}
}
}
services:
acme_demo_bundle.image_type_extension:
class: Acme\DemoBundle\Form\Extension\TextTypeExtension
tags:
- { name: form.type_extension, alias: form }
$builder->add('MyInput', 'text', array('myOption' => array('whateverYouNeed')));
将类添加为服务:
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class TextTypeExtension extends AbstractTypeExtension {
public function getExtendedType() {
return 'form'; // What type should be extended
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
// Add optional option - you can also add required options
// and available values of this option
$resolver->setOptional(array('myOption'));
}
public function buildView(FormView $view, FormInterface $form, array $options) {
// You can put any logic here
// For example 'myOption' => 'big' can be transformed to
// 'myOption' => '300px'
if (array_key_exists('myOption', $options)) {
// Add your option to twig template
$view->vars['myOption'] = $options['myOption'];
}
}
}
services:
acme_demo_bundle.image_type_extension:
class: Acme\DemoBundle\Form\Extension\TextTypeExtension
tags:
- { name: form.type_extension, alias: form }
$builder->add('MyInput', 'text', array('myOption' => array('whateverYouNeed')));
现在您可以做:
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class TextTypeExtension extends AbstractTypeExtension {
public function getExtendedType() {
return 'form'; // What type should be extended
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
// Add optional option - you can also add required options
// and available values of this option
$resolver->setOptional(array('myOption'));
}
public function buildView(FormView $view, FormInterface $form, array $options) {
// You can put any logic here
// For example 'myOption' => 'big' can be transformed to
// 'myOption' => '300px'
if (array_key_exists('myOption', $options)) {
// Add your option to twig template
$view->vars['myOption'] = $options['myOption'];
}
}
}
services:
acme_demo_bundle.image_type_extension:
class: Acme\DemoBundle\Form\Extension\TextTypeExtension
tags:
- { name: form.type_extension, alias: form }
$builder->add('MyInput', 'text', array('myOption' => array('whateverYouNeed')));
来源:
当我想添加自定义属性时,我将它们放在中,以便在细枝模板中使用
attr.myCustomOption
.hmm。但您只能传递这样的标量值,它们将以html显示。传递数组将引发:在第326行的表单_div_layout.html.twig中呈现模板(“注意:数组到字符串转换”)时引发异常
如果它是一个数组,您必须使用for
循环对其进行迭代,您不能只是尝试将其作为字符串进行回显。如果您需要,Twig有一个函数。您想让我为每个可用元素创建私有自定义元素类吗?文本、选择、实体和其他内容@这只是一个例子。这取决于你如何实施它。告诉我如何将其实现到所有现有表单类型。所以我可以使用:$builder->add('anyName','anyTypeHere',array('myOption'=>array())代码>