Question

Im using FOSRestBundle to build a REST API in Symfony.

In my tax Controller i have:

    private function processForm(Request $request, Tax $tax )
    {

        $form = $this->createForm(new TaxType(),$tax);

        $req = $request->request->all();


        $form->submit($req['tax']);



        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($tax);
            $em->flush();
            return $this->getTaxAction($tax->getId());
        }

        return array(
            'form' => $form,
            'request' => $request->request->all()
        );
    }

This function called from the POST and PUT functions.

TaxType

        class TaxType extends AbstractType
        {
                /**
             * @param FormBuilderInterface $builder
             * @param array $options
             */
            public function buildForm(FormBuilderInterface $builder, array $options)
            {
                $builder
                    ->add('createDate')
                    ->add('writeDate')
                    ->add('name')
                    ->add('amount')
                    ->add('active')
                    ->add('createUid')
                    ->add('writeUid')
                    ->add('company')
                ;
            }
    ...

It worked fine so far, but now i added some extra column to the table (and proprty to the Tax Entity) like: to which company belongs the record, date of creation. This wont come from the client but set on server side. How do i add eg createData?

I have tried

    $req['tax']['createDate'] = new \DateTime("now");

but im getting:

 {"code":400,
"message":"Validation Failed",
"errors":{"children":{"createDate":{"errors":["This value is not valid."],
"children":{"date":{"children":{"year":[],"month":[],"day":[]}},
"time":{"children":{"hour":[],"minute":[]}}}},
"writeDate":{"children":{"date":{"children":{"year":[],"month":[],"day":[]}},
"time":{"children":{"hour":[],"minute":[]}}}},"name":[],"amount":[],"active":[],"createUid":[],"writeUid":[],"company":[]}}}

from entity Tax.php

    /**
     * @var \DateTime
     */
    private $createDate;

I guess im extending the request with the correct data type, but im getting validation error.

Était-ce utile?

La solution

If I understand the question correctly you don't have to take the request but just change the property on the $tax-object. You can also do this after the form validation if you want.

Autres conseils

You can Remove add('createDate') from your form builder.

$builder
         ->add('createDate')
         ->add('writeDate')
         ->add('name')
         ->add('amount')
         ->add('active')
         ->add('createUid')
         ->add('writeUid')
         ->add('company')

Then set the createdDate value on your $tex object before persist.

if ($form->isValid()) {
    $tax->setCreateDate(new \DateTime("now"));
    $em = $this->getDoctrine()->getManager();
    $em->persist($tax);
    $em->flush();
    return $this->getTaxAction($tax->getId());
}

Alternative Method

Or you can use Doctrine's Lifecycle Callbacks to achieve this.

Happy coding!!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top