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!