Get unmapped values with Symfony forms

Viewed 27

I'm trying to create an API that takes multiple values mapped as parameters, but it's possible that this API takes other values and I need to store this information in my database.

I would like to know how I can get these unmapped values.

My form is basically like this:

<?php

namespace App\ApiController\Integration\Webhook\Proposal;

use Simonetti\MesaCredito\Proposta\Webhook\FinancingDTO;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;

class FinanciamentoType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('amortizationType', TextType::class, [
                'constraints' => [
                    new NotBlank(),
                ],
                'documentation' => [
                    'example' => 'SAC',
                ],
            ])
            ->add('assetValue', IntegerType::class, [
                'required' => true,
                'documentation' => [
                    'example' => 100,
                ],
            ])
            ->add('calculeDate', DateType::class, [
                'required' => true,
                'widget' => 'single_text',
                'input'  => 'datetime_immutable',
                'format' => DateType::HTML5_FORMAT,
            ])
            ->add('notMapped',TextType::class,['mapped'=>true]);
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'allow_extra_fields' => true,
            'data_class' => FinancingDTO::class,
        ]);
    }
}

My FinancingDTO class looks like this:

<?php

namespace Simonetti\CreditDesk\Proposal\Webhook;

class FinancingDTO implements \JsonSerializable
{
    private string $amortizationType;
    private int $assetValue;
    private \DateTimeImmutable $dataCalculo;
    private mixed $notMapped = [];

    public function setAmortizationType(string $amortizationType): self
    {
        $this->amortizationType = $amortizationType;

        return $this;
    }

    public function amortizationType(): string
    {
        return $this->amortizationType;
    }

    public function setAssetValue(int $assetValue): self
    {
        $this->assetValue = $assetValue;

        return $this;
    }

    public function assetValue(): int
    {
        return $this->valorAtivo;
    }

    public function setCalculeDate(\DateTimeImmutable $calculeDate): self
    {
        $this->calculeDate= $calculeDate;

        return $this;
    }

    public function calculeDate(): \DateTimeImmutable
    {
        return $this->calculeDate;
    }

    /**
     * @return mixed
     */
    public function getNotMapped(): mixed
    {
        return $this->notMapped;
    }

    /**
     * @param mixed $notMapped
     */
    public function setNotMapped(mixed $notMapped): self
    {
        $this->notMapped = $notMapped;

        return $this;
    }
}

I want to add the unmapped fields in notMapped Any idea how to solve it?

0 Answers
Related