1

i have a little problem with form. I have made form class and i need form field which will be like radio or select. Choices for select i need to pull from DB of another form. Here is the code so pls tell me where is my problem and how to solve it.

I want to have option when i create POST to choose from available pages (their names) and store that so i know for each post to which page it belongs and for each page i do query and show posts for that page..

<?php

 namespace AppBundle\AppForm;

 use Symfony\Component\Form\AbstractType;
 use Symfony\Component\Form\FormBuilderInterface;
 use AppBundle\Entity\Page;

 class PostForm extends AbstractType {
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
    $builder
        ->add('name', 'text')
        ->add('content', 'textarea')
        ->add('visible', 'choice', array(
            'choices' => array(
                'Yes' => 1,
                'No' => 0
            )
        ))
        ->add('belongToPage', 'choice', array(
            'choices' => array (
                new Page() 
           // here i want to pull from class Page names of 
           //all pages stored and to present
                // this names for options in form field
            ),
            'choices_as_values' => true,
            'choice_label' => 'getName' 
   //getName is function from class Page which returns name of page(s)

        ))
        ->add('save', 'submit', array('label' => 'Create Post'));
}

public function getName()
{
    // TODO: Implement getName() method.
}
}
1

1 Answer 1

1

Why don't you use entity field type. It would be smth like this:

->add('belongToPage', 'entity', array(
        'class' => 'Class\Namespace\Class',
        'property' => 'name',
        'label' => 'choice_field_label'
    ))

If you need smth more complicated then just findAll for this field, you could use query_builder option:

    ->add('belongToPage', 'entity', array(
        'class' => 'Class\Namespace\Class',
        'property' => 'name',
        'label' => 'choice_field_label',
        'query_builder' => function(EntityRepository $er) {
            return $er->findAllPagesForPostForm(); 
            //Where findAllPagesForPostForm is the name of method in your
            // pagesRepo which returns queryBuilder, 
            //instead of this you could just write your custom query like
            //$qb = $er->createQueryBuilder('p');
            //$qb->andWhere(...);
            //return $qb;
        } 
    ))
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this is the answer i was looking for.. I have found info about entity type but i didnt know you can pass property in array so it was returning whole class and i got error like can not convert class to string type.. With property i can pull exactly what i need as a string..
@Constantine1001, no problem! Just mark it as answer :)

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.