我正在尝试在Symfony 3中使用表单验证,但有一个错误:
无法加载类型" AppBundle Entity accountsheet in/var/www/teem> mywebsite /vendor/symfony/symfony/symfony/symfony/src/symfony/symfony/symfony/component/component/form/form/form/formregrregrregsistry.phppet/ppunptim>
这是定义:
src/appbundle/controller/accountsheetcontroller.php
<?php
namespace AppBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationJsonResponse;
use SymfonyComponentHttpFoundationResponse;
use FOSRestBundleControllerAnnotations as Rest;
use AppBundleFormTypeAccountSheetType;
use AppBundleEntityAccountSheet;
class AccountSheetController extends Controller{
[...]
/**
* @RestView(statusCode=Response::HTTP_CREATED)
* @RestPost("/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 form type type accountsheettype.php
<?php
namespace AppBundleFormType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use AppBundleEntityAccountSheet;
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/resources/config/valivation.yml
AppBundleEntityAccountSheet:
properties:
name:
- NotBlank: ~
- Type: string
app/config/config.yml
framework:
validation: { enabled: true, enable_annotations: false }
我可能错过了一些东西,但是Symfony对我来说仍然是新的,我没有想法。
更改
$form = $this->createForm(AccountSheet::class, $aSheet);
to
$form = $this->createForm(AccountSheetType::class, $aSheet);