0

I am trying to pass a parameter from a FORM to a twig, that is different from the one where I make my data input. If this explanation is messy - here's more simplified version... I have just started to study php+symfony so please don't hurt me too hard...

I have two empty fields on a "CREATE" page, I fill them with say AAA and BBB each, I want AAA and BBB to appear on another "FORMZ" page.

I am NOT using any database, so no need to use ObjectManager etc, it's only to understand how everything's working...

I have created a ArticleFormType.php file

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder

        ->add('title', TextType::class, [
            'attr' =>   [
                'placeholder' => "title from ArticleFormType",
                'class' => 'form-control'
                        ]
        ])
        ->add('content')

        ->add('save', SubmitType::class, [
            'label' => 'SAVE'
        ])
    ;
}


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

and that is my Controller:

 /**
 * @Route("/formz", name="formz")
 */
public function index()
{


    $title = 'Created by DT';


    return $this->render('formz/index.html.twig', [
       'title' => $title
    ]);
}

/**
 * @Route("/formz/create", name="create")
 */
public function create (Request $request, ObjectManager $manager ) {


        $article = new MyClassArticle();
        $task = new ArticleFormType();

        $form = $this->createForm(ArticleFormType::class, $article, [
            'action' => $this->generateUrl('create'),
        ]);





        $form->handleRequest($request);

    dump($article);

    if($form->isSubmitted() && $form->isValid()) {

            dump($article);


            return $this->redirectToRoute('formz');


    }



    return $this->render('formz/create.html.twig', [
        'formTest' => $form->createView()
    ]);

}

I don't get how can I transfer my $article to a public function index() - I heard it can be done somehow (in the case above) by passing parameters from public function create() to public function index(). Can anybody help me with that please?

I am thanking you in advance!

  • Store your data somewhere and then retrieve. – u_mulder Mar 26 '19 at 16:03
  • Possible duplicate of [Redirect to page with parameters in symfony](https://stackoverflow.com/questions/29499806/redirect-to-page-with-parameters-in-symfony) https://stackoverflow.com/questions/1496975/symfony-redirect-with-2-parameters – Krzysztof Raciniewski Mar 27 '19 at 06:46

1 Answers1

0

you need to store the article somewhere, ideally through doctrine (entity save)

then you need to update your index route to be something like this

@Route("/formz/{article}", name="formz")
function index(Artile $article = null)

Then you can use the following with redirectToRoute

$this->redirectToRoute("formz", array("article" => $article))

That should solve your issue.

Andy
  • 679
  • 2
  • 10
  • 25
  • Thanks a lot for your reply! The problem is that I don't know how to strore the article... it's been only couple of days since I started learning symfony... can you please explain? Thank you. – Dmitrii Trubetskoy Mar 26 '19 at 16:20
  • your best bet is to read this: https://symfony.com/doc/current/doctrine.html the symfony docs are amazing and will answer pretty much everything you want to know. it'll be quicker for you to digest this than me explaining. – Andy Mar 26 '19 at 16:41
  • Thank you! The small issue is that I am not using any database for the moment. My class has no @Route or whatever... Well, I suppose I am wrong but the problem is that FOR ME it looks like I am not supposed to use any Doctrine for the moment... But ok, I'll do my best to understand... – Dmitrii Trubetskoy Mar 27 '19 at 08:18
  • then you wont be able to do what you want as how will you be able to pass the data? you could, i guess, have the article stored in a session and then get that from the session. but that seems somewhat convoluted – Andy Mar 27 '19 at 11:22
  • Hi Andy, so that was a solution: return $this->index($article); in controller and just a public function index($article) in a Controller... Then I just return $this->render('formz/index.html.twig', [ 'article' => [$article] ]); and this seems to work after all... (And thanks for your kind reply - it gave me some clues!) – Dmitrii Trubetskoy Mar 28 '19 at 12:22