Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!