Populating a Symfony2 form with a JSON request

i am having issues populating a symfony2 form, there are some relationships.

class PackageType extends AbstractType
{

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('delivery_note')
            ->add('receiver', new ReceiverType())
            ->add('package_lines', 'collection', array(
                'type'         => new PackageLineType(),
                'allow_add'    => true,
                'by_reference' => false,
            ));
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'IREnterpriseAppBundleEntityPackage',
            'csrf_protection'   => false,
            'allow_extra_fields' => true
        ));
    }

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

As you see this uses the receiver type, and package_lines as a collection.
The receiver object gets populated properly, but the package_lines die silenty, here is the form type for that.

class PackageLineType extends AbstractType {
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->setMethod('POST')
            ->add('quantity')
        /*
            ->add('stockitem', 'entity', array(
                'class' => 'IREnterpriseAppBundleEntityStockItem',
                'property' => 'id'
        */
        ;
    }


    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'IREnterpriseAppBundleEntityPackageLine',
            'csrf_protection' => false,
            'allow_extra_fields' => true
        ));
    }

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

I commented the stockitem, since that would only complicate matters further, since that is supposed to be an id, that references a existing stock item, anyway, the quantity witch is just an integer is not working either.

Here is my controller:

/**
 * @RestView
 */
public function newPackageAction(Request $request)
{
    $package = new Package();
    $form = $this->createForm(new PackageType(), $package);

    $form->handleRequest($request);

    return View::create($package);
 }

When i call form->isValid it returns true, but the returned $package object, is stripped of package_lines, even tho they are there with the rest of the request which looks like this:

REQUEST:

{"package":{"receiver":{"name":"fadafdadffad","company":"faddafafdfdafda","address1":"affdadfaafddaf","country":"DK","zip_code":"7100","city":"Vejle","phone":"23322332","email":"dafadffdfda@live.dk"},"package_lines":{"0":{"stockitem":"6","quantity":"1"},"1":{"stockitem":"6","quantity":"1"}}}}

RESPONSE:

{"receiver":{"name":"fadafdadffad","company":"faddafafdfdafda","address1":"affdadfaafddaf","country":"DK","zip_code":"7100","city":"Vejle","phone":"23322332","email":"dafadffdfda@live.dk"},"package_lines":{},"created_at":"2015-07-12T08:12:54+0200","status":"PROCESSING"}

The package lines are not set, why is this?

I have read the docs, and in the email example they give here:
http://symfony.com/doc/current/reference/forms/types/collection.html

They send the collection as emails[0] emails[1] etc

My package_lines are sendt as package_lines[0], package_lines[1], etc


Source: forms

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.