FOSRestBundle icon indicating copy to clipboard operation
FOSRestBundle copied to clipboard

How do I get entity data from the Form Type?

Open 12th opened this issue 10 years ago • 0 comments

I would like to use the Form Type to upload the entity data to the client and their further validation when upgrading held in one place.

But when the pass in the view a form with the entity data, the response data itself is not only the name of entities and properties with a blank value.

How can I get the data from the form?

AccountRestController

<?php

namespace App\CoreBundle\Controller;

...

/**
 * @Rest\RouteResource("Account", pluralize=false)
 */
class AccountRestController extends FOSRestController
{
    /**
     * @ApiDoc(
     *   resource = true,
     *   description = "User data",
     *   statusCodes = {
     *     200 = "Returned when successful",
     *     404 = "Returned when the user is not found"
     *   }
     * )
     */
    public function getProfileAction()
    {
        /** @var $user User */
        $user = $this->getUser();

        $response['form'] = $this->createForm(new ProfileType(), $user);
        $view = $this->view($response, 200);

        return $this->handleView($view);
    }
}

Profile Form Type

<?php

namespace App\CoreBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class ProfileType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('username', TextType::class)
            ->add('email', TextType::class)
        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'App\UserBundle\Entity\User',
            'csrf_protection' => false,
        ));
    }

    /**
     * @return string
     */
    public function getBlockPrefix()
    {
        return 'profile';
    }
}

Config

framework:
    ...
    serializer:
        enabled: true

...

fos_rest:
    disable_csrf_role: ROLE_API
    access_denied_listener:
        json: true
    unauthorized_challenge: "Basic realm=\"Restricted Area\""
    allowed_methods_listener: true
    routing_loader:
        default_format: json
        include_format: false
    view:
        view_response_listener: force
    zone:
        - { path: ^/api/* }

composer.json

...
"require": {
        "php": ">=5.3.3",
        "symfony/symfony": "2.8.2",
        "symfony/assetic-bundle": "dev-master",
        "symfony/swiftmailer-bundle": "v2.3.11",
        "symfony/monolog-bundle": "2.8.2",

        "doctrine/orm": "v2.5.2",
        "doctrine/doctrine-bundle": "1.6.1",
        "doctrine/doctrine-cache-bundle": "1.2.2",

        "twig/extensions": "1.3.0",

        "sensio/distribution-bundle": "v3.0.34",
        "sensio/framework-extra-bundle": "v3.0.13",
        "sensio/buzz-bundle": "dev-master",

        "incenteev/composer-parameter-handler": "v2.1.2",
        "knplabs/knp-paginator-bundle": "2.4.*@dev",

        "friendsofsymfony/user-bundle": "dev-master",
        "leaseweb/api-caller-bundle": "1.0.*@dev",
        "misd/phone-number-bundle": "v1.0.5",
        "hwi/oauth-bundle": "0.4.0",

        "gregwar/image-bundle": "dev-master",
        "gregwar/captcha-bundle": "v2.0.2",
        "egeloen/ckeditor-bundle": "4.0.1",
        "doctrine/doctrine-migrations-bundle": "1.1.1",
        "symfony/yaml": "v2.8.2",
        "friendsofsymfony/rest-bundle": "2.0.x-dev",
        "friendsofsymfony/jsrouting-bundle": "^1.6"
    },
...    

Response

{
  "username": [],
  "email": []
}

screenshot at 22-27-57

12th avatar May 29 '16 19:05 12th