1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
<?php
namespace App\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\FormError;
class DefaultController extends Controller
{
public function formAction()
{
/** @var \Symfony\Component\HttpFoundation\RequestStack $requestStack */
$requestStack = $this->container->get('request_stack');
/** @var \Symfony\Component\HttpFoundation\Request $request */
$request = $requestStack->getMasterRequest();
/** @var \Symfony\Component\Form\FormBuilder $fb */
$fb = $this->createFormBuilder(array(),array());
$fb->add('name','text');
$fb->add('send','submit');
/** @var \Symfony\Component\Form\Form $form */
$form = $fb->getForm();
$form->handleRequest($request);
if($form->isValid())
{
// display error for test
$form->get('name')->addError(new FormError('name error'));
}
return $this->render('AppDemoBundle:Default:form.html.twig',array(
'form'=>$form->createView()
));
}
} |
Partager