Question

Request JSON:

{"vpnusers": {"expire_account": "2017-11-27 16:28:15", "status_id": 1}}

The result printed (in the controller):

print_r($request->get('vpnusers'));
Array
(
    [expire_account] => 2017-11-27 16:28:15
    [status_id] => 1
)

VpnUsersType:

namespace Hoax\PartnerBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class VpnUsersType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('status_id')
            ->add('expire_account')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Hoax\PartnerBundle\Entity\VpnUsers'
            , 'csrf_protection' => false
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'vpnusers';
    }
}

I have just followed this guide and can see there's no such issue as I have: http://williamdurand.fr/2012/08/02/rest-apis-with-symfony2-the-right-way/

Was it helpful?

Solution

You cannot set data_class option `Hoax\PartnerBundle\Entity\VpnUsers' since what you are submitting is not an object.

Either set that to NULL or create an object from request data and pass it to you form.

In case of NULL you are very close:

  1. set data_class to null
  2. In controller do the following:

    $form = $this->createForm(new VpnUsersType(), $request->get('vpnusers'));
    

In case you go for object type:

  1. leave data_class be
  2. In controller:

    $req_data = $request->get('vpnusers');
    
    // either fetch from database **or** creare new instance here, your call...
    $vpnUser = $this->getDoctrine()->getRepository('HoaxPartnerBundle:VpnUsers')->find($req_data['status_id']); // IS THIS OK? Not sure really...
    $form = $this->createForm(new VpnUsersType(), $vpnUser);
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top