Guest User

Untitled

a guest
Sep 18th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. How to set a default value in a Symfony 2 form field?
  2. class QuotesType extends AbstractType
  3. {
  4. private $id;
  5.  
  6. public function __construct($id){
  7. $this->product_id = $id;
  8. }
  9.  
  10. public function buildForm(FormBuilder $builder, array $options)
  11. {
  12. $builder
  13. ->add('user_name', 'text')
  14. ->add('user_lastname', 'text')
  15. ->add('user_email', 'email')
  16. ->add('user_comments', 'textarea')
  17. ->add('user_product_id', 'hidden', array(
  18. 'data' => $this->product_id,
  19. ));
  20. ;
  21. }
  22.  
  23. namespace QNMainBundleEntity;
  24.  
  25. use DoctrineORMMapping as ORM;
  26. use SymfonyComponentValidatorConstraints as Assert;
  27.  
  28. /**
  29. * QNMainBundleEntityQuotes
  30. *
  31. * @ORMTable()
  32. * @ORMEntity(repositoryClass="QNMainBundleEntityQuotesRepository")
  33. */
  34. class Quotes
  35. {
  36.  
  37. public function __construct($p_id)
  38. {
  39. $this->date = new Datetime('today');
  40. }
  41. /**
  42. * @var integer $id
  43. *
  44. * @ORMColumn(name="id", type="integer")
  45. * @ORMId
  46. * @ORMGeneratedValue(strategy="AUTO")
  47. */
  48. private $id;
  49.  
  50. /**
  51. * @var integer $user_product_id
  52. *
  53. * @ORMColumn(name="user_product_id", type="integer")
  54. */
  55. private $user_product_id = "1";
  56.  
  57. /**
  58. * @var datetime $date
  59. *
  60. * @ORMColumn(name="date", type="datetime")
  61. */
  62. private $date;
  63.  
  64. public function requestAction($id)
  65. {
  66. $repository = $this->getDoctrine()
  67. ->getEntityManager()
  68. ->getRepository('QNMainBundle:Categories');
  69. $categories = $repository->findAll();
  70.  
  71. $quote = new Quotes($id);
  72. $form = $this->createForm(new QuotesType(), $quote);
  73.  
  74. $formHandler = new QuotesHandler($form, $this->get('request'), $this->getDoctrine()->getEntityManager());
  75.  
  76. if( $formHandler->process() )
  77. {
  78. return $this->redirect( $this->generateUrl('QNMain_Product', array('id' => $id)) );
  79. }
  80.  
  81. return $this->render('QNMainBundle:Main:requestaform.html.twig', array(
  82. 'categories' => $categories,
  83. 'id' => $id,
  84. 'form' => $form->createView(),
  85. ));
  86.  
  87. }
  88.  
  89. namespace QNMainBundleForm;
  90.  
  91. use SymfonyComponentFormForm;
  92. use SymfonyComponentHttpFoundationRequest;
  93. use DoctrineORMEntityManager;
  94. use QNMainBundleEntityQuotes;
  95.  
  96. class QuotesHandler
  97. {
  98. protected $form;
  99. protected $request;
  100. protected $em;
  101.  
  102. public function __construct(Form $form, Request $request, EntityManager $em)
  103. {
  104. $this->form = $form;
  105. $this->request = $request;
  106. $this->em = $em;
  107. }
  108.  
  109. public function process()
  110. {
  111. if( $this->request->getMethod() == 'POST' )
  112. {
  113. $this->form->bindRequest($this->request);
  114.  
  115. if( $this->form->isValid() )
  116. {
  117. $this->onSuccess($this->form->getData());
  118.  
  119. return true;
  120. }
  121. }
  122.  
  123. return false;
  124. }
  125.  
  126. public function onSuccess(Quotes $quote)
  127. {
  128. $this->em->persist($quote);
  129. $this->em->flush();
  130. }
  131.  
  132. }
  133.  
  134. /**
  135. * @ORMAnnotations...
  136. */
  137. private $user_product_id = 9000;
Add Comment
Please, Sign In to add comment