0

I have a simple table with 3 columns with some (<10) rows.

Table:

/**
 * @ORM\Entity
 * @ORM\Table(name="property_addition_cost_frequency")
 */
class PropertyAdditionCostFrequency
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */private $id;

    /**
     * @ORM\Column(type="string")
     */
    private $label;

    /**
     * @ORM\Column(type="string")
     */
    private $group;

    /**
     * @ORM\Column(type="string")
     */
    private $active;
    //all getters and setters
}

I have a working code for showing 1 row in a prefilled form.

AdminCoreDataController.php

/**
 * @Route("/core_data/{id}", defaults={"id" = 1}, name="admin_core_data")
 */
public function showCoreDataListAction( Request $request, PropertyAdditionCostFrequency $propertyAdditionCostFrequency )
{
    $form = $this->createForm( PropertyAdditionCostFrequencyForm::class, $propertyAdditionCostFrequency);
    $form->handleRequest($request);

    if($form->isSubmitted() && $form->isValid()){
        $propertyAdditionCostFrequency  = $form->getData();
        $em = $this->getDoctrine()->getManager();
        $em->persist($propertyAdditionCostFrequency);
        $em->flush();
        $this->addFlash('success', 'successfully changed the data');
        return $this->redirectToRoute('admin_property_list');
    }

    return $this->render('logged_in/content/admin/core_data/core_data.html.twig', [
        'propertyCostFrequencyForm' => $form->createView()
    ]);
}

core_data.html.twig

{{ form_start(propertyCostFrequencyForm) }}
    {{ form_widget(propertyCostFrequencyForm.label) }}
    {{ form_widget(propertyCostFrequencyForm.active) }}
    <button type="submit">send</button>
{{ form_end(propertyCostFrequencyForm) }}

PropertyAdditionCostFrequencyForm.php

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add(  'label' )
        ->add(  'active' )
    ;
}

How can I get a form with all rows from the table if no "id" is given?

3
  • 3
    Start with form collections: symfony.com/doc/current/form/form_collections.html Commented Jan 6, 2017 at 15:02
  • If I understand right, you want to edit all the PropertyAdditionCostFrequency in the same time? or do you want to have 10 forms, one for each row? Commented Jan 6, 2017 at 15:56
  • @goto yes, i want to edit all rows at the same time in one form Commented Jan 9, 2017 at 8:06

1 Answer 1

1

Cerad was right, symfony.com/doc/current/form/form_collections.html was a good place to start. Thank you! After some changes to the code i got

Table:

class PropertyAdditionCostFrequency
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */private $id;

    /**
     * @ORM\Column(type="string")
     */
    private $label;

    /**
     * @ORM\Column(type="integer")
     */
    private $group;

    /**
     * @ORM\Column(type="boolean")
     */
    private $active;

    //getter and setter
}

AdminCoreDataController.php

public function showCoreDataListAction( Request $request )
{
    $repository = $this->getDoctrine()->getRepository('AppBundle:PropertyAdditionCostFrequency');
    $cost_frequency = $repository->findAll();

    $PropertyAdditionCostFrequency = new PropertyAdditionCostFrequency();

    foreach ($cost_frequency as $k => $v) {
        $PropertyAdditionCostFrequency->getLabel()->add($v);
    }

    $form = $this->createForm( PropertyAdditionCostFrequencyForm::class, $PropertyAdditionCostFrequency);
    $form->handleRequest($request);

    if($form->isSubmitted() && $form->isValid()){
        $em = $this->getDoctrine()->getManager();
        $em->flush();
        $this->addFlash('success', 'successfully changed the data');
        return $this->redirectToRoute('admin_core_data');
    }

    return $this->render('logged_in/content/admin/core_data/core_data.html.twig', [
        'propertyCostFrequencyForm' => $form->createView()
    ]);
}

core_data.html.twig

{{ form_start(propertyCostFrequencyForm) }}
    {% for single_frequency in propertyCostFrequencyForm.label %}
        <div class="row">
            <div class="col-sm-4">
                {{ form_widget(single_frequency.label) }}
            </div>
            <div class="col-sm-1">
                {{ form_widget(single_frequency.active) }}
            </div>
        </div>
    {% endfor %}
<button type="submit">send</button>
{{ form_end(propertyCostFrequencyForm) }}

PropertyAdditionCostFrequencyForm.php

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add(  'label', CollectionType::class, array(
            'entry_type' => PropertyAdditionCostFrequencyType::class
        ))
        ->add(  'active', CollectionType::class, array(
            'entry_type' => PropertyAdditionCostFrequencyType::class
        ))
    ;
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => PropertyAdditionCostFrequency::class
    ]);
}

PropertyAdditionCostFrequencyType.php

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('label')
        ->add('active', CheckboxType::class, array(
            'required' => false,
        ))
    ;
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => PropertyAdditionCostFrequency::class,
    ));
}
Sign up to request clarification or add additional context in comments.

Comments

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.