Skip to content

Instantly share code, notes, and snippets.

@eddymugwiza eddymugwiza/FormController Secret
Created Mar 7, 2018

Embed
What would you like to do?
<?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

This comment has been minimized.

Copy link
Owner Author

commented Mar 7, 2018

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
You can’t perform that action at this time.