0
votes

I'm trying to submit data to form, but after submitting my compound field have empty value. The problem is with a field propertyValues.

My form:

class PropertyForm extends FormType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);
        $builder
            ->add('name', TextType::class)
            ->add('propertyValues', CollectionType::class, [
                'entry_type' => PropertyValueDTO::class,
                'required' => true,
                'constraints' => [
                    Count::class, ['min' => 1]
                ]
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        parent::configureOptions($resolver);
        return $resolver
            ->setDefaults([
                'data_class' => PropertyDTO::class
            ])
        ;
    }
}

Submitted data:

Array
(
    [propertyValues] => Array
        (
            [0] => Array
                (
                    [id] => 15
                    [value] => Ikea
                    [propertyId] => 7
                    [productsCount] => 4
                )

        )

    [id] => 7
    [name] => Manufacturer
    [description] => 
)

Resulted form data:

App\DTO\Api\PropertyDTO Object
(
    [id:App\DTO\Api\PropertyDTO:private] => 
    [name:App\DTO\Api\PropertyDTO:private] => Manufacturer
    [description:App\DTO\Api\PropertyDTO:private] => 
    [propertyValues:App\DTO\Api\PropertyDTO:private] => Array
        (
        )

)

and the form is submitted and valid. As You see, propertyValues is an empty array

My controller code:

public function update(Property $property, Request $request)
{
    $form = $this->createForm(PropertyForm::class);
    $data = json_decode($request->getContent(), true);
    $form->submit($data);
    if (!$form->isValid()) {
        return $this->jsonFormFailed($form);
    }

    return $this->jsonResponse($form->getData());
}

I have tried to trace function submit() and stumbled upon such a code:

$viewData = $this->config->getCompound() ? $this->viewData : $submittedData;

As my field is compound symfony uses empty $this->viewData instead of $submittedData

1

1 Answers

0
votes

I forgot to add option allow_add. Now all works fine