Skip to content

Instantly share code, notes, and snippets.

@eddymugwiza
Created March 7, 2018 22:34
<?php
namespace AppBundle\Controller;
use AppBundle\Form\UserForm;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class FormControllerTest extends Controller
{
/**
* @Route("register", name="register_form")
*/
public function createSimpleForm()
{
$form = $this->createForm(UserForm::class, array('action'=> $this->generateUrl("add_new_user")));
return $this->render('views/users/users.html.twig',
[
'userForm' => $form->createView()
]
);
}
/**
* @Route("user/add", name="add_new_user")
* @param Request $request
*/
public function processForm(Request $request)
{
$form = $this->createForm(UserForm::class,array('action'=> $this->generateUrl("add_new_user")));
$form->handleRequest($request);
if ($form->isSubmitted()) {
dump($form->getData());die;
}
}
}
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class UserForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
// dump($options);die;
$builder
->add('name')
->add('last_name')
->add('something_else')
->setAction($options['data']['action'])
->setMethod('POST');
}
public function configureOptions(OptionsResolver $resolver)
{
}
}
@eddymugwiza
Copy link
Author

Second method in above controller serves for processing from

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment