Сообщество IT-специалистов
Ответы на любые вопросы об IT
Профессиональное развитие в IT
Удаленная работа для IT-специалистов
Best Practice Define your forms as PHP classes.
class HomeController extends Controller { /** * @Route("/") */ public function indexAction(Request $request) { $book = new Book(); $form = $this->createFormBuilder($book) //............. } }
Да, я так и делаю
class HomeController extends Controller { /** * @Route("/") */ public function indexAction(Request $request) { $book = new Book(); $form = $this->createForm(BookType::class, $book); $form->handleRequest($request); if ($form->isSubmitted() AND $form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($book); $em->flush(); // дописываем информацию про идентификатор клиента $this->get('session')->set('book_id', $book->getId()); return $this->redirectToRoute('app_book_transfer', array( 'form' => $form, 'request' => $request )); } return $this->render('AppBundle:Book:find.html.twig', array( 'add_selects' => $additionalSelects, 'form' => $form->createView(), )); } }
class BookType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('date', DateTimeType::class, array('widget' => 'single_text')) ->add('fromBook') ->add('toBook') ->add('people'); } /** * @param OptionsResolver $resolver */ public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'data_class' => 'AppBundle\Entity\Book' )); } }