2
votes

I'm trying to use form validation in Symfony 3 but got this error:

Could not load type \"AppBundle\Entity\AccountSheet\ in /var/www/mywebsite/vendor/symfony/symfony/src/Symfony/Component/Form/FormRegistry.php

Here are the definitions:

src/AppBundle/Controller/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\Form\Type\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/Resources/config/validation.yml

AppBundle\Entity\AccountSheet:
    properties:
        name:
            - NotBlank: ~
            - Type: string

app/config/config.yml

framework:
    validation:      { enabled: true, enable_annotations: false }

I probably missed something but Symfony is still new to me and I'm out of ideas.

1
Can you try replacing data_class value in configureOptions method (src\AppBundle\Form\Type\AccountSheetType.php) with the AccountSheet class namespace ? AppBundle\Entity\AccountSheetCatalin Minovici
Already tried, same result :/AnomalySmith

1 Answers

1
votes

Change

$form = $this->createForm(AccountSheet::class, $aSheet);

to

$form = $this->createForm(AccountSheetType::class, $aSheet);