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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
* @Method("GET")
*/
public function indexAction()
{
$form = $this->getForm();
return $this->render('default/index.html.twig',array('form'=>$form->createView()));
}
/**
* @Route("/check",name="submit")
* @Method("POST")
*/
public function submitAction(Request $request)
{
$form = $this->getForm();
$form->handleRequest($request);
if($form->isValid()){
return new JsonResponse(array('text'=>$form->getData()));
}
$errors = array();
foreach($form->getErrors(true) as $error){
$errors[] = $error->getMessage();
}
return new JsonResponse(array('errors'=>$errors,'data'=>print_r($form->getData(),true)));
}
protected function getForm()
{
return $this->createFormBuilder(array(),array(
'action'=>$this->generateUrl('submit')
))
->add('text','text')
->add('submit','submit')
->getForm();
}
} |