Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
Symfony forms & json objects validation

I'm trying to create an object of type, say, Slot related to an object of other type, Customer. Here is how the post request body looks like.

  {
       "name: "foo
       "customer": {id: 21}
  }

here is how I get the data and build the form:

$data = json_decode($request->getContent(), true);
$entity = new Slot();

$entityManager = $this->getDoctrine()->getManager();
$form = $this->createForm(SlotType::class, $entity);
    
$form->submit($data, true);

zer

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder

          ->add('customer', EntityType::class, [
                         "class" => Customer::class
           ]);

The form I use to submit this data uses an EntityType form type, but it seems it can't recognize the data posted for the customer field and tells me that the value is not valid for this field. Do I need to preprocess the data in the controller to populate my entity with associated entities? Or is there a way to attach dataTransformers to the SlotType to populate the form with valid data ?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can use a DTO with properties (slotTypeName and customerId), the form will be easier and after the validation you use this DTO to build your entities

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda