我刚开始使用symfony表单,我不明白,为什么我会遇到这种类型的错误: screen from profiler
那么我认为应该通过什么来使其正常运作? 这是我在RegisterController中的代码:
/**
* @Route("/register", name="register")
*/
public function showRegisterAction()
{
$registration = new User();
$form = $this->createForm(new Register(), $registration, ['action' => $this->generateUrl('create'), 'method' => 'POST']);
return $this->render("/login/register.html.twig", ['form' => $form->createView()]);
}
和我的注册类的代码与fromBuilder:
class Register extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', 'text', ['label' => 'USERNAME'])
->add('mail', 'email', ['label' => 'email'])
->add('password', 'password', ['label' => 'password'])
->add('password_repeat', 'password', ['mapped' => false, 'label' => 'confirm'])
->add('company', 'text', ['label' => 'company'])
->add('nip', 'text', ['label' => 'nip'])
->add('street', 'text', ['label' => 'street'])
->add('street_nr', 'text', ['label' => 'street_nr'])
->add('postal_code', 'text', ['label' => 'postal_code'])
->add('city', 'text', ['label' => 'city'])
->add('tel', 'text', ['label' => 'tel'])
->getForm();
}
public function getName()
{
return "register";
}
public function setDefaultOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(['data_class' => 'AppBundle\Entity\User']);
}
}
答案 0 :(得分:0)
$form = $this->createForm(new Register(), $registration, ['action' => $this->generateUrl('create'), 'method' => 'POST']);
方法期望第一个参数是表单类名(字符串),而不是它的实例。
所以改变这一行:
$form = $this->createForm(
Register::class,
$registration,
['action' => $this->generateUrl('create'), 'method' => 'POST']
);
到
getForm()
另请注意,在创建表单类时,您不需要[<class 'main.models.A'>, <class 'main.models.A2'>]
FormBuilder的方法。
供参考:http://symfony.com/doc/current/forms.html#creating-form-classes