8

long story short: Using FOSRestBundle I'm trying to create some entities via POST call, or modify existing via PUT.

here the code:

/**
 * Put action
 * @var Request $request
 * @var integer $id Id of the entity
 * @return View|array
 */
public function putCountriesAction(Request $request, $id)
{
    $entity = $this->getEntity($id);
    $form = $this->createForm(new CountriesType(), $entity, array('method' => 'PUT'));
    $form->bind($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();
        return $this->view(null, Codes::HTTP_NO_CONTENT);
    }

    return array(
        'form' => $form,
    );
} //[PUT] /countries/{id}

If I call /countries/{id} with PUT passing a json like {"description":"Japan"}, it modify my country with id=1, putting an empty description.

If, instead, I try to create a NEW entity with this method:

/**
 * Create new Countries (in batch)
 * @param  Request $request json request
 * @return array           redirect to get_coutry, will show the newly created entities
 */
public function postCountriesAction(Request $request)
{
    $entity = new Countries();
    $form = $this->createForm(new CountriesType(), $entity);
    $form->bind($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        return $this->redirectView(
            $this->generateUrl(
                'get_country',
                array('id' => $entity->getId())
            ),
            Codes::HTTP_CREATED
        );
    }

    return array(
        'form' => $form,
    );
} //[PUT {"description":"a_description"}] /countries

it gives me an error saying:

exception occurred while executing 'INSERT INTO countries (description) VALUES (?)' with params [null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'description' cannot be null

so seems that i'm not able to pass correctly the request to bind to the form.

Notice that if i json_decode the request as suggested here it reply with a

{
    "code":400,
    "message":"Validation Failed",
    "errors":{
        "errors":[
            "This value is not valid."
        ],
        "children":{
            "description":[
            ]
        }
    }
}

Any Advice?

Thanks, Rolls

4

2 に答える 2

21

私は解決しました:)

これが、以前は機能しなかった理由です。

私のフォーム定義では、名前は「zanzibar_backendbundle_countries」でした。

public function getName()
{
    return 'zanzibar_backendbundle_countries';
}

したがって、リクエストをこのフォームにバインドするには、json は次のようになります。

{"zanzibar_backendbundle_countries": [{"description": "Japan"}]}

のようなものにしたかったので

{"id":1,"description":"Italy"}

フォームから名前を削除する必要がありました:

public function getName()
{
    return '';
}

一般に、次のようなプレースホルダーを使用してjsonを投稿する場合

"something":{"key":"value"}

フォーム名は正確に「何か」でなければなりません

于 2013-12-14T15:14:33.213 に答える