0

I am looking for a way to implement multiple data-transformers for one entity.

I am adding forms in a FormType like this:

//RegistrationFormType.php

...

$sectionsForm = $factory->createNamed('sections', 'form', null, array(
    'label' => false,
    'auto_initialize' => false
));


foreach ($sections as $key=>$section) {

    $sectionform = $factory->createNamed('section_'.$section->getId(),'form', null, array(
        'label' => $section->getName(),
    ));

    $formQuestions = $section->getFormQuestions();
    foreach ($formQuestions as $formQuestion) {

        $sectionform->add('question_'.$formQuestion->getId(), 'text',array('attr'=>...));

        ...        

    }

    $sectionsForm->add($sectionform);
}

$form->add($sectionsForm);

$builder->addModelTransformer(new RegistrationFormToArrayTransformer($em));

Now in the ModelTransformer, I am transforming the RegistrationForm entity like this:

$sections['section_'.$sectionId]['question_'.$questionId] = ...

Is there a more elegant way to do this? My source code is getting quite messy and was wondering if I can somehow have multiple array-transformers, one for each entity.

1 Answer 1

1

You can add a transformer on each fields yes :

   $sectionform->add(
      $sectionform
         ->create('question_'.$formQuestion->getId(), 'text',array('attr'=>...))
         ->addModelTransformer(/* your transformer */)
   );
Sign up to request clarification or add additional context in comments.

1 Comment

actualy i am getting an error with this: Attempted to call method "addModelTransformer" on class "Symfony\Component\Form\Form".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.