我正在尝试在Symfony 3中使用表单验证但是出现了这个错误:
无法加载类型\“AppBundle \ Entity \ AccountSheet \ in /var/www/mywebsite/vendor/symfony/symfony/src/Symfony/Component/Form/FormRegistry.php
以下是定义:
的src /的appbundle /控制器/ AccountSheetController.php
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\Controller\Annotations as Rest;
use AppBundle\Form\Type\AccountSheetType;
use AppBundle\Entity\AccountSheet;
class AccountSheetController extends Controller{
[...]
/**
* @Rest\View(statusCode=Response::HTTP_CREATED)
* @Rest\Post("/account/{account_id}/sheet")
*/
public function postAccountSheetsAction($account_id, Request $request){
$account = $this->get('doctrine.orm.entity_manager')
->getRepository('AppBundle:Account')
->find($account_id);
if(empty($account))
return $this->notFound('AccountSheet');
$aSheet = new AccountSheet();
$aSheet->setAccount($account);
$form = $this->createForm(AccountSheet::class, $aSheet);
$form->submit($request->request->all());
if ($form->isValid()) {
$em = $this->get('doctrine.orm.entity_manager');
$em->persist($aSheet);
$em->flush();
return $aSheet;
}
return $form;
}
[...]
}
SRC \的appbundle \表格\型号\ AccountSheetType.php
<?php
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use AppBundle\Entity\AccountSheet;
class AccountSheetType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name');
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => AccountSheet::class,
'csrf_protection' => false
]);
}
}
的src /的appbundle /资源/配置/ validation.yml
AppBundle\Entity\AccountSheet:
properties:
name:
- NotBlank: ~
- Type: string
应用/配置/ config.yml
framework:
validation: { enabled: true, enable_annotations: false }
我可能错过了一些东西,但Symfony对我来说还是新手,而且我没有想法。
答案 0 :(得分:1)
更改
$form = $this->createForm(AccountSheet::class, $aSheet);
到
$form = $this->createForm(AccountSheetType::class, $aSheet);