PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 12, 2022

[FIXED] How do I send "extra" data to a Symfony 3.2.x form using FormBuilder?

 March 12, 2022     php, symfony, symfony-forms     No comments   

Issue

I have the following method in a Symfony 3.2.7 controller:

public function updateAction(Request $request)
{
    $em        = $this->getDoctrine()->getManager();
    // this is the original entity without modified values
    $entity    = $em->getRepository('QuoteBundle:Quote')->find($request->query->get('id'));

    // the entity passed to the form has the values modified coming from the request    
    $form = $this->createForm(CFProgramLevelQuoteType::class, $entity);
    ...
}

And this is the form:

class CFProgramLevelQuoteType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('CFProgramLevel', EntityType::class, [
                'class'         => 'QuoteBundle:CFProgramLevel',
                'choice_label'  => 'description',
                'placeholder'   => 'Please select a program',
            ]);
    }
    ...
}

I will use a Query Builder to filter some values from QuoteBundle:CFProgramLevel so I need to get the unmodified ID from the $entity and send it to the form. This is the solution I have in mind so far:

public function updateAction(Request $request)
{
    $em        = $this->getDoctrine()->getManager();
    $entity    = $em->getRepository('QuoteBundle:Quote')->find($request->query->get('id'));
    $entity_id = $entity->getCfProgramLevel()->getcfProgramLevelId();   
    $form = $this->createForm(CFProgramLevelQuoteType::class, $entity);
    ...
}

But how I can pass that entity_id to the form and used it there? If there is a better way to achieve this I am open to hear ideas. I couldn't find anything helpful in Google so any help is appreciated.

Update

Trying the solution provided in the answer below I couldn't get it to work either:

// controller
$form = $this->createForm(CFProgramLevelQuoteType::class, $entity, ['entity_id' => $entity_id]);

// form
public function buildForm(FormBuilderInterface $builder, array $options)
{
    dump($options);
    die();
    ...
}

public function setDefaultOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(['data_class' => Quote::class, 'entity_id' => null]);
    $resolver->setRequired('entity_id');
}

The option "entity_id" does not exist. Defined options are: "action", "allow_extra_fields", "attr", "auto_initialize", "block_name", "by_reference", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled", "empty_data", "error_bubbling", "error_mapping", "extra_fields_message", "inherit_data", "invalid_message", "invalid_message_parameters", "label", "label_attr", "label_format", "mapped", "method", "post_max_size_message", "property_path", "required", "translation_domain", "trim", "upload_max_size_message", "validation_groups".


Solution

You can create your own custom options for form type like described on http://symfony.com/doc/current/form/create_custom_field_type.html#defining-the-field-type

class CFProgramLevelQuoteType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        //$options['entity_id'] contains your id
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setRequired('entity_id');
    }
}

Pass entity_id:

$form = $this->createForm(CFProgramLevelQuoteType::class, $entity, [
    'entity_id' => $entity_id
]);


Answered By - striker
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing