2017-04-26 86 views
0

我剛開始用symfony形式,我不明白,爲什麼我得到這個類型的錯誤: screen from profiler這個createForm參數有什麼問題?

所以schould我通過什麼作爲參數,使其正常工作? 下面是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

createForm方法需要的第一個參數是窗體類名(字符串),而不是一個它的實例。

因此改變這一行:

$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() FormBuilder的方法。

僅供參考:http://symfony.com/doc/current/forms.html#creating-form-classes

+0

感謝您的幫助:)! – Eddy

+0

不客氣。另外請注意,在創建表單類時,您不需要'getForm()'FormBuilder的方法。 –