如何在 Symfony 2 表单字段中设置默认值? [英] How to set a default value in a Symfony 2 form field?
问题描述
我一直在尝试使用 Symfony 2
设置表单.
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)
但现在我需要自动填写一个字段,我听说我必须在 ProductType.php 中填写,在那里创建表单(用于我的产品).
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.
但我不知道该怎么做,这是我在 ProductType.php 中的 buildForm 函数:
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,
));
;
}
它显然不起作用,因为我收到一个 SQL 错误,说我的字段为空.
and it obviously doesnt work since I got a SQL error saying that my field is null.
如何为 user_product_id 设置默认值?我应该直接对对象做吗?
How can I put a default value to the user_product_id ? should I do it directly to the object ?
这是我的实体代码的一部分:
Here is a part of the code of my entity :
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();
}
}
我还把我尝试在实体中设置的 Date
放在这里,我可能在这两种情况下都做错了,因为我不能让它工作,也不能......日期不在buildForm
函数,我不知道我是否应该..
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 ..
推荐答案
你在这里试图做的是创建一个安全漏洞:任何人都可以在 user_product_id
字段中注入任何 ID并欺骗你的申请.更不用说渲染一个字段而不显示它是没用的.
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.
您可以在实体中为 user_product_id
设置默认值:
You can set a default value to user_product_id
in your entity:
/**
* @ORMAnnotations...
*/
private $user_product_id = 9000;
这篇关于如何在 Symfony 2 表单字段中设置默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!