Kosolapov Vadim
Kosolapov Vadim

Reputation: 35

Fields not present in Request are set to null in entity

I make rest api controller. I have a form SliderApiType, in which I pass the fields that I want to edit. If I pass only one field, for example 'label', then only this field will be changed, and all other fields in this form will be null.

Му form:

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('label', TextType::class, ['label' => 'Заголовок', 'required' => false])
            ->add('description', TextType::class, ['label' => 'Описание', 'required' => false])
            ->add('serialNumber', IntegerType::class, ['label' => 'Номер', 'required' => false])
            ->add('picture', IntegerType::class, [
                'invalid_message' => 'picture is not a valid',
            ])
            ->add('send', SubmitType::class, ['label' => 'Отправить'])
        ;

        $builder->get('picture')
            ->addModelTransformer($this->transformer);
    }

My controller in wherein I make edit action:

    public function edit(Slider $slideId, Request $request, EntityManagerInterface $em)
    {
        $form = $this->createForm(SliderApiType::class, $slideId);
        $data = json_decode($request->getContent(), true);
        $form->submit($data);
        if (!($form->isSubmitted() && $form->isValid())) {
            return $this->handleView($this->view($form->getErrors(true)));
        }

        $em = $this->getDoctrine()->getManager();
        $em->persist($slideId);
        $em->flush();

        $context = new Context();
        $context->addGroup('slider_create');
        $view = $this->view($slideId);
        $view->setContext($context);

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

If pass this fields in json:

{
  "label": "string",
  "description": "string",
  "picture": 1
}

All working good!

But if I pass this json:

{
  "label": "string"
}

Field "label" set how "string", but fields "description" and "picture" sets null.

How to make it so that if I passed the field, only it was edited, and the other two fields were not null?

Upvotes: 0

Views: 359

Answers (2)

JessGabriel
JessGabriel

Reputation: 1082

I suggest you to put this logic directly in your entity. Like that, you are confident that whenever you update the same information, it will apply the same logic instead of only in the controller. Something like this for each fields:

public function setField(string $newValue)
{
    if (!empty($newValue)) {
        $this->field = $newValue;
    }

    return $this;
}

Upvotes: 0

ejuhjav
ejuhjav

Reputation: 2710

I haven't written any rest apis, but with little googling it looks like you can just use the second parameter of the submit function:

$form->submit($data, false);

This is assuming that your function here is used only for updating existing entity data (PATCH/PUT method) - if you want to support multiple methods in the same action then you need to check the request method type:

$clearMissing = $request->getMethod() != 'PATCH';
$form->submit($data, $clearMissing);

Upvotes: 1

Related Questions