[英]Symfony3 create form without entity in type file
I have created a form in my controller like this :我在我的控制器中创建了一个表单,如下所示:
$data = array ();
$formBuilder = $this->createFormBuilder ( $data );
$formBuilder->add( 'Field1', NumberType::class, array(
'constraints' => array(
new NotBlank(),
),
) )
->add ( 'Field2', NumberType::class, array(
'constraints' => array(
new NotBlank(),
)
...);
$form = $formBuilder->getForm ();
I am trying to put my form creation in a Type file.我试图将我的表单创建放在一个类型文件中。 I did this like this but the form is not created and I can't display form fields in my view.I don't understand why.
我是这样做的,但没有创建表单,我无法在视图中显示表单字段。我不明白为什么。
#in ControlController
$data = array ();
$formBuilder= $this->createFormBuilder(ControlType::class, $data);
$form = $formBuilder->getForm ();
#in ControlType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add( 'Field1', NumberType::class, array(
'constraints' => array(
new NotBlank(),
),
) )
->add ( 'Field2', NumberType::class, array(
'constraints' => array(
new NotBlank(),
)....;
}
Edit 1 : I have tried all things you tell me, but it still doesn't work.编辑1:我已经尝试了你告诉我的所有事情,但它仍然不起作用。 My code looks like this now :
我的代码现在看起来像这样:
#in ControlController
$data = ['Field1' => null, 'Field2' => null];
$formBuilder= $this->createFormBuilder(ControlType::class, $data);
$form = $formBuilder->getForm ();
return $this->render ( 'MeTestBundle:Control:index.html.twig', array (
'form' => $form->createView ()
) );
#in ControlType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add( 'Field1', NumberType::class, array(
'mapped' => false,
) )
->add ( 'Field2', NumberType::class, array(
'constraints' => array(
new NotBlank(),
),
'mapped' => false
))
->add ( 'save', SubmitType::class, array (
'label' => 'Control'
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Control::class,
));
}
But now the error I have is:但现在我的错误是:
The options "Field1", "Field2" do not exist. Defined options are: "action",
"allow_extra_fields", "attr", "auto_initialize", "block_name", "by_reference",
"compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection",
"csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled",
"empty_data", "error_bubbling", "error_mapping", "extra_fields_message",
"inherit_data", "invalid_message", "invalid_message_parameters", "label",
"label_attr", "label_format", "mapped", "method", "post_max_size_message",
"property_path", "required", "translation_domain", "trim",
"upload_max_size_message", "validation_groups".
Your Controller Code to create the Form seems to be wrong.您创建表单的控制器代码似乎是错误的。
The Signature for createFormBuilder in Controller is defined like: Controller 中 createFormBuilder 的签名定义如下:
public function createFormBuilder($data = null, array $options = array())
What you want should be:你想要的应该是:
$data = ['Field1' => null, 'Field2' => null];
$form = $this->createForm(ControlType::class, $data);
return $this->render ( 'MeTestBundle:Control:index.html.twig', array (
'form' => $form->createView ()
));
Edit: Also you should not set a data_class
if you're not gonna use a data object.编辑:此外,如果您不打算使用数据对象,则不应设置
data_class
。 Just leave configureOptions
empty in this case.在这种情况下,只需将
configureOptions
留空即可。 If you want to use an Entity you should pass an instance of it as $data
instead of a simple array.如果你想使用一个实体,你应该将它的一个实例作为
$data
而不是一个简单的数组传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.