Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace AppBundle\Controller;
- use AppBundle\Entity\Test;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Symfony\Component\Form\Extension\Core\Type\SubmitType;
- use Symfony\Component\HttpFoundation\Request;
- class DefaultController extends Controller
- {
- /**
- * @Route("/test/", name="test")
- */
- public function testAction(Request $request)
- {
- $test = new Test();
- $test->setPrice(45);
- $test->setName('Nowa książka o PHP');
- $validator = $this->get('validator');
- $errors = $validator->validate($test);
- if (count($errors) > 0) {
- return $this->render('default/errors.html.twig', ['errors' => $errors]);
- }
- $em = $this->getDoctrine()->getEntityManager();
- $em->persist($test);
- $em->flush($test);
- return $this->render('default/test.html.twig');
- }
- /**
- * @Route("/test-form/", name="test_form")
- */
- public function testFormAction(Request $request)
- {
- $test = new Test();
- $form = $this->createFormBuilder($test)
- ->add('name')
- ->add('price')
- ->add('save', SubmitType::class, array('label' => 'Create Post'))
- ->getForm();
- $form->handleRequest($request);
- if ($form->isValid()) {
- $test = $form->getData();
- $em = $this->getDoctrine()->getEntityManager();
- $em->persist($test);
- $em->flush($test);
- return $this->render('default/formOk.html.twig', ['form' => $form->createView()]);
- }
- return $this->render('default/form.html.twig', ['form' => $form->createView()]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement