访问 Symfony2 请求对象中的 POST 值

发布于 2024-11-27 16:34:41 字数 537 浏览 0 评论 0原文

好吧,这是一个新手问题,但我在任何地方都找不到答案。在 Symfony2 的控制器中,我想从我的表单之一访问 POST 值。在控制器中,我有:

public function indexAction()
{ 
    $request = $this->get('request');
    if ($request->getMethod() == 'POST') {
        $form = $this->get('form.factory')->create(new ContactType());
        $form->bindRequest($request);
        if ($form->isValid()) {
            $name_value = $request->request->get('name');

不幸的是 $name_value 没有返回任何内容。我做错了什么?谢谢!

OK, this is a newbie question, but I can't find the answer anywhere. In a controller in Symfony2, I want to access the POST value from one of my forms. In the controller I have:

public function indexAction()
{ 
    $request = $this->get('request');
    if ($request->getMethod() == 'POST') {
        $form = $this->get('form.factory')->create(new ContactType());
        $form->bindRequest($request);
        if ($form->isValid()) {
            $name_value = $request->request->get('name');

Unfortunately $name_value isn't returning anything. What am I doing wrong? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

臻嫒无言 2024-12-04 16:34:41

表单发布值存储在请求中表单的名称下。例如,如果您已重写 ContactType() 的 getName() 方法以返回“contact”,则可以执行以下操作:

$postData = $request->request->get('contact');
$name_value = $postData['name'];

如果仍然遇到问题,请尝试执行 var_dump $request->request->all() 上的 () 查看所有发布值。

The form post values are stored under the name of the form in the request. For example, if you've overridden the getName() method of ContactType() to return "contact", you would do this:

$postData = $request->request->get('contact');
$name_value = $postData['name'];

If you're still having trouble, try doing a var_dump() on $request->request->all() to see all the post values.

浅语花开 2024-12-04 16:34:41

Symfony 2.2

此解决方案自 2.3 起已弃用,并将在 3.0 中删除,查看文档

$form->getData();

表单参数的数组

为您提供来自 symfony2 book 第 162 页(第 12 章:表格)

[...] 有时,您可能只想使用没有类的表单,并返回提交的数组
数据。这实际上非常简单:

public function contactAction(Request $request) {
  $defaultData = array('message' => 'Type your message here');
  $form = $this->createFormBuilder($defaultData)
  ->add('name', 'text')
  ->add('email', 'email')
  ->add('message', 'textarea')
  ->getForm();
  if ($request->getMethod() == 'POST') {
    $form->bindRequest($request);
    // data is an array with "name", "email", and "message" keys
    $data = $form->getData();
  }
  // ... render the form
}

您还可以直接通过请求对象访问 POST 值(在本例中为“name”),如下所示:

$this->get('request')->request->get('name');

但是,请注意,在大多数情况下使用 getData() 方法更好选择,因为它
返回被表单框架转换后的数据(通常是一个对象)。

当你想访问表单令牌时,你必须使用Problematic的答案
$postData = $request->request->get('contact'); 因为 getData() 从数组


Symfony 2.3< /strong>

从 2.3 开始,您应该使用 handleRequest 而不是 bindRequest

 $form->handleRequest($request);

查看文档

Symfony 2.2

this solution is deprecated since 2.3 and will be removed in 3.0, see documentation

$form->getData();

gives you an array for the form parameters

from symfony2 book page 162 (Chapter 12: Forms)

[...] sometimes, you may just want to use a form without a class, and get back an array of the submitted
data. This is actually really easy:

public function contactAction(Request $request) {
  $defaultData = array('message' => 'Type your message here');
  $form = $this->createFormBuilder($defaultData)
  ->add('name', 'text')
  ->add('email', 'email')
  ->add('message', 'textarea')
  ->getForm();
  if ($request->getMethod() == 'POST') {
    $form->bindRequest($request);
    // data is an array with "name", "email", and "message" keys
    $data = $form->getData();
  }
  // ... render the form
}

You can also access POST values (in this case "name") directly through the request object, like so:

$this->get('request')->request->get('name');

Be advised, however, that in most cases using the getData() method is a better choice, since it
returns the data (usually an object) after it's been transformed by the form framework.

When you want to access the form token, you have to use the answer of Problematic
$postData = $request->request->get('contact'); because the getData() removes the element from the array


Symfony 2.3

since 2.3 you should use handleRequest instead of bindRequest:

 $form->handleRequest($request);

see documentation

半﹌身腐败 2024-12-04 16:34:41

对我有用的是使用这个:

$data = $request->request->all();
$name = $data['form']['name'];

what worked for me was using this:

$data = $request->request->all();
$name = $data['form']['name'];
烟火散人牵绊 2024-12-04 16:34:41

ParameterBag::get() 方法有一个技巧。您可以设置 $deep< /code> 参数true 并访问所需的深层嵌套值,无需额外变量:

$request->request->get('form[some][deep][data]', null, true);

您还可以设置默认值(第二个参数get() 方法),可以避免冗余的 isset($form['some']['deep']['data']) 调用。

There is one trick with ParameterBag::get() method. You can set $deep parameter to true and access the required deep nested value without extra variable:

$request->request->get('form[some][deep][data]', null, true);

Also you have possibility to set a default value (2nd parameter of get() method), it can avoid redundant isset($form['some']['deep']['data']) call.

骄傲 2024-12-04 16:34:41

可以通过以下方式在控制器中访问现场数据:
示例12-34

$form->get('dueDate')->getData();

另外,未映射字段的数据也可以直接修改:
清单12-35

$form->get('dueDate')->setData(new \DateTime());

第164页symfony2书(2013年10月9日生成)

The field data can be accessed in a controller with:
Listing 12-34

$form->get('dueDate')->getData();

In addition, the data of an unmapped field can also be modified directly:
Listing 12-35

$form->get('dueDate')->setData(new \DateTime());

page 164 symfony2 book(generated on October 9, 2013)

风筝在阴天搁浅。 2024-12-04 16:34:41

我通过以下方式访问多部分发布请求的 TicketNumber 参数。

$data = $request->request->all();
$ticketNumber = $data["ticketNumber"];

I access the ticketNumber parameter for my multipart post request in the following way.

$data = $request->request->all();
$ticketNumber = $data["ticketNumber"];
春夜浅 2024-12-04 16:34:41

我认为为了获取由表单对象绑定和验证的请求数据,您必须使用:

$form->getClientData();

I think that in order to get the request data, bound and validated by the form object, you must use :

$form->getClientData();

请帮我爱他 2024-12-04 16:34:41

Symfony doc 获取请求数据

最后,可以使用 getContent() 访问随请求正文发送的原始数据:

$content = $request->getContent();

Symfony doc to get request data

Finally, the raw data sent with the request body can be accessed using getContent():

$content = $request->getContent();

最好是你 2024-12-04 16:34:41

如果您是新手,欢迎来到 Symfony2,这是一个开源项目,所以如果您想学习很多东西,您可以开源!

来自“Form.php”:

getData()
获取标准数据()
getViewData()

您可以在此文件中找到更多详细信息。

If you are newbie, welcome to Symfony2, an open-source project so if you want to learn a lot, you can open the source !

From "Form.php" :

getData()
getNormData()
getViewData()

You can find more details in this file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文