Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How to set a default value in a Symfony 2 form field?
- class QuotesType extends AbstractType
- {
- private $id;
- public function __construct($id){
- $this->product_id = $id;
- }
- public function buildForm(FormBuilder $builder, array $options)
- {
- $builder
- ->add('user_name', 'text')
- ->add('user_lastname', 'text')
- ->add('user_email', 'email')
- ->add('user_comments', 'textarea')
- ->add('user_product_id', 'hidden', array(
- 'data' => $this->product_id,
- ));
- ;
- }
- namespace QNMainBundleEntity;
- use DoctrineORMMapping as ORM;
- use SymfonyComponentValidatorConstraints as Assert;
- /**
- * QNMainBundleEntityQuotes
- *
- * @ORMTable()
- * @ORMEntity(repositoryClass="QNMainBundleEntityQuotesRepository")
- */
- class Quotes
- {
- public function __construct($p_id)
- {
- $this->date = new Datetime('today');
- }
- /**
- * @var integer $id
- *
- * @ORMColumn(name="id", type="integer")
- * @ORMId
- * @ORMGeneratedValue(strategy="AUTO")
- */
- private $id;
- /**
- * @var integer $user_product_id
- *
- * @ORMColumn(name="user_product_id", type="integer")
- */
- private $user_product_id = "1";
- /**
- * @var datetime $date
- *
- * @ORMColumn(name="date", type="datetime")
- */
- private $date;
- public function requestAction($id)
- {
- $repository = $this->getDoctrine()
- ->getEntityManager()
- ->getRepository('QNMainBundle:Categories');
- $categories = $repository->findAll();
- $quote = new Quotes($id);
- $form = $this->createForm(new QuotesType(), $quote);
- $formHandler = new QuotesHandler($form, $this->get('request'), $this->getDoctrine()->getEntityManager());
- if( $formHandler->process() )
- {
- return $this->redirect( $this->generateUrl('QNMain_Product', array('id' => $id)) );
- }
- return $this->render('QNMainBundle:Main:requestaform.html.twig', array(
- 'categories' => $categories,
- 'id' => $id,
- 'form' => $form->createView(),
- ));
- }
- namespace QNMainBundleForm;
- use SymfonyComponentFormForm;
- use SymfonyComponentHttpFoundationRequest;
- use DoctrineORMEntityManager;
- use QNMainBundleEntityQuotes;
- class QuotesHandler
- {
- protected $form;
- protected $request;
- protected $em;
- public function __construct(Form $form, Request $request, EntityManager $em)
- {
- $this->form = $form;
- $this->request = $request;
- $this->em = $em;
- }
- public function process()
- {
- if( $this->request->getMethod() == 'POST' )
- {
- $this->form->bindRequest($this->request);
- if( $this->form->isValid() )
- {
- $this->onSuccess($this->form->getData());
- return true;
- }
- }
- return false;
- }
- public function onSuccess(Quotes $quote)
- {
- $this->em->persist($quote);
- $this->em->flush();
- }
- }
- /**
- * @ORMAnnotations...
- */
- private $user_product_id = 9000;
Add Comment
Please, Sign In to add comment