简体   繁体   English

无法加载类型提交symfony 3

[英]could not load type submit symfony 3

I'm a newbie on Symfony and I have some problems with the 3thd version. 我是Symfony的新手,而3thd版本存在一些问题。 I created a formType with the console called userType. 我使用名为userType的控制台创建了一个formType。 Everything is ok but I can't add a submit button. 一切正常,但我无法添加提交按钮。 The old way ->add('save','submit') is not working and I think it's about the version of symfony. 旧方法->add('save','submit')不起作用,我认为这与symfony的版本有关。 So, here is my code : 所以,这是我的代码:

 <?php namespace MyBundle\\UserBundle\\Form; use Symfony\\Component\\Form\\AbstractType; use Symfony\\Component\\Form\\FormBuilderInterface; use Symfony\\Component\\OptionsResolver\\OptionsResolver; class userType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('name') ->add('nickname') //->add('dateOfBirth', 'date') ->add('sex') ->add('country') ->add('city') ->add('email') ->add('password') ->add('save', SubmitType::class, array('label' => 'Create Task')); //here is the problem } /** * @param OptionsResolver $resolver */ public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'data_class' => 'MyBundle\\UserBundle\\Entity\\user\u0026#39; )); } } 

Have you got an idea about how to resolve that problem ? 您知道如何解决该问题吗? Sorry if I'm posting in the wron section :-). 抱歉,如果我在wron部分中发布:-)。

Thank's for having read me, 谢谢你读我

Axel 阿克塞尔

You need to use correct namespace: 您需要使用正确的名称空间:

namespace MyBundle\UserBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; // ← this line

As stated on page 27 of the book 'Best practices for Symfony 3' you can find that this is not changed. 《 Symfony 3的最佳实践 》一书第27页所述,您会发现它没有改变。

Form Button Configuration 表单按钮配置

Form classes should try to be agnostic to where they will be used. 表单类应尽量避免使用它们。 This makes them easier to re-use later. 这使它们以后更易于重用。 Add buttons in the templates, not in the form classes or the controllers. 在模板中而不是在表单类或控制器中添加按钮。 Since Symfony 2.3, you can add buttons as fields on your form. 从Symfony 2.3开始,您可以将按钮添加为表单上的字段。 This is a nice way to simplify the template that renders your form. 这是简化呈现表单的模板的好方法。 But if you add the buttons directly in your form class, this would effectively limit the scope of that form. 但是,如果直接在窗体类中添加按钮,则将有效地限制该窗体的范围。

$builder
   // ...
   ->add('save', SubmitType::class, array('label' => 'Create Post'))
;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM