Symfony 3 - 子表单和元素名称
Symfony 3 - subform and element name
我想用这样的组合模式创建表单:
https://symfony.com/doc/current/form/inherit_data_option.html
我使用 Symfony 3。
它正在运行。我让每个元素都像单个对象并添加这个。
但最后我的表单元素名称变成了
form[subform][element]
如何在名称属性中制作没有子表单的平面结构?
use AppBundle\Base\Form\NickType;
use AppBundle\Base\Form\MailType;
use AppBundle\Base\Form\PassType;
use AppBundle\Base\Form\UserType;
class RegisterType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nick', NickType::class)
->add('mail', MailType::class)
->add('password', PassType::class)
->add('repeat_password', PassType::class)
(etc...)
和单元素
class NickType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nick', TextType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'inherit_data' => true
));
}
}
如果 NickType
仅继承 TextType,则无需定义它。您可以删除 NickType
、MailType
等
你可以这样做:
class RegisterType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nick', TextType::class)
;
(etc...)
如果你想重用一个表单域,你必须创建一个 Custom Form Field Type:
class NickType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
//...
));
}
public function getParent()
{
return TextType::class;
}
}
您可以从元素名称中删除 form[]
,但实际上并不推荐删除它,因为当您读取填充表单数据的请求时,您可以通过表单名称来识别表单。 (via)
You can set the name of the root form to empty, then your field name
will be just form. Do so via
// the first argument to createNamedBuilder() is the name
$form = $this->get('form.factory')->createNamedBuilder(null, 'form', $defaultData)
->add('from', 'date', array(
'required' => false,
'widget' => 'single_text',
'format' => 'dd.MM.yyyy'
));
(via)
我想用这样的组合模式创建表单: https://symfony.com/doc/current/form/inherit_data_option.html
我使用 Symfony 3。
它正在运行。我让每个元素都像单个对象并添加这个。
但最后我的表单元素名称变成了
form[subform][element]
如何在名称属性中制作没有子表单的平面结构?
use AppBundle\Base\Form\NickType;
use AppBundle\Base\Form\MailType;
use AppBundle\Base\Form\PassType;
use AppBundle\Base\Form\UserType;
class RegisterType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nick', NickType::class)
->add('mail', MailType::class)
->add('password', PassType::class)
->add('repeat_password', PassType::class)
(etc...)
和单元素
class NickType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nick', TextType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'inherit_data' => true
));
}
}
如果 NickType
仅继承 TextType,则无需定义它。您可以删除 NickType
、MailType
等
你可以这样做:
class RegisterType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nick', TextType::class)
;
(etc...)
如果你想重用一个表单域,你必须创建一个 Custom Form Field Type:
class NickType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
//...
));
}
public function getParent()
{
return TextType::class;
}
}
您可以从元素名称中删除 form[]
,但实际上并不推荐删除它,因为当您读取填充表单数据的请求时,您可以通过表单名称来识别表单。 (via)
You can set the name of the root form to empty, then your field name will be just form. Do so via
// the first argument to createNamedBuilder() is the name
$form = $this->get('form.factory')->createNamedBuilder(null, 'form', $defaultData)
->add('from', 'date', array(
'required' => false,
'widget' => 'single_text',
'format' => 'dd.MM.yyyy'
));
(via)