我有一个 .twig 文件,其格式如下:
{% block section_contents %}
<section id="main" class="aux manage signup clearfix">
<div class="container welcome-back arvo-regular clearfix">
Join the Mysite Community
</div>
<div class="container form-bg clearfix">
<form class="content-form wrap"
action="{{ path('MysiteMainBundle_signup_additional_info') }}"
method="post" {{ form_enctype(additionalInfoForm) }} novalidate>>
{% if formValid is defined %}
</br></br>
<div class="alert alert-block alert-error fade in">
<h4> Errors </h4>
{{ form_errors(settingsForm.firstName) }}
{{ form_errors(settingsForm.lastName) }}
{{ form_errors(settingsForm.address.addressOne) }}
{{ form_errors(settingsForm.email) }}
{{ form_errors(settingsForm.shop.name) }}
{{ form_errors(settingsForm) }}
</div>
{% endif %}
<div class="personal-details">
<div class="text-line clearfix">
<div class="sub-header">
First Name
</div>
{{ form_widget(additionalInfoForm.firstName, { 'attr': {'class': 'validate[required] arvo-regular' } }) }}
</div>
<div class="text-line clearfix">
<div class="sub-header">
Last Name
</div>
{{ form_widget(additionalInfoForm.lastName, { 'attr': {'class': 'validate[required] arvo-regular' } }) }}
</div>
<input type="submit" value="Sign Up" class="arvo-regular button green signup-button" />
</div> <!-- end login details -->
</form>
</div>
</section>
{% endblock section_contents %}
但是,当我提交此表格时,我得到:
Fatal error: Cannot instantiate abstract class Mysite\MainBundle\Entity\Document in /Mysite/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/Type/FormType.php on line 172 Call Stack: 0.0011 659400 1. {main}() /Mysite/web/app_dev.php:0 0.0284 7412872 2. Symfony\Component\HttpKernel\Kernel->handle() /Mysite/web/app_dev.php:29 0.1012 13005672 3. Symfony\Bundle\FrameworkBundle\HttpKernel->handle() /Mysite/app/bootstrap.php.cache:619 0.1013 13007328 4. Symfony\Component\HttpKernel\HttpKernel->handle() /Mysite/app/bootstrap.php.cache:1568 0.1013 13007328 5. Symfony\Component\HttpKernel\HttpKernel->handleRaw() /Mysite/app/bootstrap.php.cache:1392 0.2212 28737544 6. call_user_func_array() /Mysite/app/bootstrap.php.cache:1428 0.2212 28738032 7. Mysite\MainBundle\Controller\SignupController->indexAction() /Mysite/app/bootstrap.php.cache:1428 0.2960 35454528 8. Symfony\Component\Form\Form->bind() /Mysite/src/Mysite/MainBundle/Controller/SignupController.php:49 0.2968 35509648 9. Symfony\Component\Form\Form->bind() /Mysite/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php:539 0.2971 35517024 10. Symfony\Component\Form\Extension\Core\Type\{closure}() /Mysite/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php:559
我不完全确定为什么。它肯定与文档类有关。
编辑:
添加了一些表单代码:
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
switch ($options['mode']) {
case 'login':
$builder
->add('email')
->add('password')
;
break;
case 'shop_create':
$builder
->add('firstName', 'text')
->add('lastName', 'text')
->add('phone', 'text')
->add('email', 'text')
->add('address', new AddressType())
->add('city', 'text')
->add('state', 'text')
->add('dni', 'text')
->add('zipcode', 'text')
->add('shop', new ShopType())
;
break;
case 'account_settings':
$builder
->add('profilePicture', new ProfilePictureFormType())
->add('firstName', 'text')
->add('lastName', 'text')
->add('birthday', 'date', array('widget'=>'single_text'))
->add('website', 'text')
->add('biography', 'textarea')
->add('email', 'text')
->add('address', new AddressType())
->add('shop', new ShopType())
;
break;
case 'additional_signup_info':
$builder
->add('profilePicture', new ProfilePictureFormType())
->add('firstName', 'text')
->add('lastName', 'text')
->add('email', 'text')
->add('address', new AddressType())
->add('city', 'text')
->add('state', 'text')
->add('zipcode', 'text')
->add('password')
;
break;
default:
throw new \Exception('You need to select which type of user form type you want to use.');
break;
}
}
这是控制器中的表单代码:
public function indexAction(Request $request)
{
if ($request->hasSession()){
$session = $request->getSession();
$username = $session->get('username');
$session->invalidate();
}
$em = $this->getDoctrine()->getEntityManager();
$userRepository = $em->getRepository('MysiteUserBundle:User');
$user = $userRepository->findOneBy(array('username' => $username));
$additionalInfoForm = $this->createForm(new UserType(), $user, array(
'mode' => 'additional_signup_info'
));
if ($request->isMethod('POST')) {
$additionalInfoForm->bind($request);
if ($additionalInfoForm->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$em->persist($user);
$em->flush();
return $this->redirect($this->generateUrl('MysiteMainBundle_marketplace'));
}else{
return array(
'formValid' => false,
'additionalInfoForm' => $additionalInfoForm->createView()
);
}
}
return array('user' => $user, 'additionalInfoForm' => $additionalInfoForm->createView());
}