我試圖使用表單驗證中的Symfony 3,但得到這個錯誤:Symfony的3表單驗證:未能加載類型
Could not load type \"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的是對我來說還是新的,我沒有想法。
你可以嘗試configureOptions方法更換data_class值(SRC \的appbundle \表格\型號\ AccountSheetType.php)與AccountSheet類的命名空間? 'AppBundle \ Entity \ AccountSheet' –
已經嘗試過,結果相同:/ – AnomalySmith