Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8176759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:19:12+00:00 2026-06-06T23:19:12+00:00

I’ve been trying to set up a form with Symfony 2 . So I

  • 0

I’ve been trying to set up a form with Symfony 2.

So I followed the tutorial and I’ve created a special class for creating the form and handling the validation process outside the controller (as shown in the documentation)

But now I need to fill in a field automatically, I’ve heard that I have to do it in the ProductType.php, where the form (for my product) is created.

But I don’t know how to do, here is my buildForm function in ProductType.php :

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,
            ));
        ;
    }

and it obviously doesnt work since I got a SQL error saying that my field is null.

How can I put a default value to the user_product_id ? should I do it directly to the object ?

EDIT:

Here is a part of the code of my entity :

namespace QN\MainBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * QN\MainBundle\Entity\Quotes
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="QN\MainBundle\Entity\QuotesRepository")
 */
class Quotes
{

    public function __construct($p_id)
    {
        $this->date = new \Datetime('today');
    }
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var integer $user_product_id
     *
     * @ORM\Column(name="user_product_id", type="integer")
     */
    private $user_product_id = "1";

    /**
     * @var datetime $date
     *
     * @ORM\Column(name="date", type="datetime")
     */
    private $date;

And my controller :

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(),
    ));

}

My Handler :

namespace QN\MainBundle\Form;

use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManager;
use QN\MainBundle\Entity\Quotes;

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();
    }

}

I’ve also put here the Date I try to set up in the entity, I might do something wrong in both case since I can’t make it work neither ..Date is not in the buildForm function, I don’t know if I should ..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:19:13+00:00Added an answer on June 6, 2026 at 11:19 pm

    What you’re trying to do here is creating a security hole: anyone would be able to inject any ID in the user_product_id field and dupe you application. Not mentioning that it’s useless to render a field and to not show it.

    You can set a default value to user_product_id in your entity:

    /**
     * @ORM\Annotations...
     */
    private $user_product_id = 9000;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.