首页 \ 问答 \ Symfony2嵌入表单值(Symfony2 Embed form values)

Symfony2嵌入表单值(Symfony2 Embed form values)

我有自己的实用程序CalculatorType,用于计算我的文档的值(我正在使用ODM),而不是对另一个文档或子数组|对象的引用。 它有简单的2输入:

 $builder
            ->add('price', 'text', array(
                'label' => false,
                'data' => isset($options['data']) ? $options['data']->getPrice() : '0.00'
            ))
            ->add('count', 'integer', array(
                'label' => false,
                'data' => isset($options['data']) ? $options['data']->getCount() : '10000'
            ));

在父母形式我有:

$builder->
 ... // multiple fields
 ->add('calculator', 'calculator');

因此,当我尝试保存表单时,出现错误:

Neither the property "calculator" nor one of the methods

要跳过设置计算器字段,我已将mapped => false添加到选项中

 ->add('calculator', 'calculator', array('mapped' => false));

并添加了eventlistener来转换计算器数据

    $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
        $data = $event->getData();
        $data["price"] = $data["calculator"]["price"];
        $data["count"] = $data["calculator"]["count"];
        unset($data["calculator"]);
        $event->setData($data);
    });

现在表单提交值但计算器字段没有传递给嵌入表单,因为未设置$ data ['calculator']

如果我评论未unset($data["calculator"]); 然后我有一个错误

This form should not contain extra fields 

所以我找不到任何方法让这个表格起作用。 有任何想法吗?


I have my own utility CalculatorType for just calculating values for my document(I am using ODM), not reference for another document or sub arrays|object. It has simple 2 inputs:

 $builder
            ->add('price', 'text', array(
                'label' => false,
                'data' => isset($options['data']) ? $options['data']->getPrice() : '0.00'
            ))
            ->add('count', 'integer', array(
                'label' => false,
                'data' => isset($options['data']) ? $options['data']->getCount() : '10000'
            ));

In parent form I have:

$builder->
 ... // multiple fields
 ->add('calculator', 'calculator');

So when I am trying to save my form, I have an error:

Neither the property "calculator" nor one of the methods

To skip setting calculator field, I've added mapped => false to options

 ->add('calculator', 'calculator', array('mapped' => false));

and added eventlistener to transform calculator data

    $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
        $data = $event->getData();
        $data["price"] = $data["calculator"]["price"];
        $data["count"] = $data["calculator"]["count"];
        unset($data["calculator"]);
        $event->setData($data);
    });

Now form submits values but calculator fields not passing to embed form, because of unsetting $data['calculator']

If I comment unset($data["calculator"]); then I have an error

This form should not contain extra fields 

So I can't find any way to make this form work. Any ideas?


原文:https://stackoverflow.com/questions/22757366
更新时间:2022-06-04 10:06

最满意答案

您可以考虑客户端身份验证流程或使用带有OAuth对话框的 JS-SDK ,这样您可以轻松避免指定redirect_uri因为它可能由JS-SDK自动提供(或者您可以使用当前URL window.location ,如客户端文档中所示)侧认证流程)。

笔记:

虽然这可以帮助你避免使用redirect_uri实际问题更深一点......

redirect_uri使用将使这种流程难以实现,不仅因为无法预测它,而且由于要求redirect_uri应该位于App Domain中 ,因此使用JS-SDK也是如此。

应用设置截图

因此,通常您需要在应用程序设置中放置redirect_uri / URL应用程序运行的域名,这在许多客户端/域的情况下是令人讨厌的。

您可以通过使用单独的(可公开访问的)主机来实现身份验证流程,但在执行此操作之前,最好先问自己几个问题:

  1. 谁将对该主机负责,如果该主机出现问题,所有客户将会遇到什么情况。
    • 这是额外的依赖,最好避免。
  2. 您是否会在应用程序设置中为所有客户提供域名?
    • 这可能导致违反向第三方传输数据的平台政策(在此之前咨询公司律师)
  3. 您是否需要为所有客户使用单一应用程序?
    • 如果不是,您最好指示客户端设置应用程序并使用他们获得的凭据配置您的应用程序/代码。

总结一下:
您可以为每个客户端创建单独的应用程序,或指示客户端将应用程序设置为应用程序的安装/设置过程的一部分。 稍后您可以使用客户端身份验证流程来创建适用于每个客户端的通用代码(这也适用于服务器端流程,但需要一些额外的工作,并且使用JS-SDK FB.login它可能是一个插入功能没有任何额外的工作)。


You may consider Client Side authentication flow or using JS-SDK with OAuth Dialog, that way you may easily avoid specifying redirect_uri since it may be provided automatically by JS-SDK (or you may use current URL window.location as shown in documentatio of Client Side auth flow).

Notes:

While this may help you to avoid usage of redirect_uri actual problem is a bit deeper...

Usage of redirect_uri will make such flow hard to implement not only due to inability to predict it, but due to requirement that redirect_uri should be located within App Domain, same goes for usage of JS-SDK.

Application Settings screenshot

So generally you will be required to place the domain name of redirect_uri / URL Application Running on in the application settings, which is nasty in case of many clients/domains.

You may implement auth flow by using separate (publicly accessible) host but it's good to ask yourself a couple of question before doing so:

  1. Who will be responsible for that host and what will happen with all your clients if something going wrong with that host for auth only.
    • It's additional dependency which is better to avoid.
  2. Will you be albe to provide domains for all of your clients in application settings?
    • This may lead to violation of platform policies on data transfer to third parties (consult a company lawyer before doing so)
  3. Are you required to use single Application for all your clients?
    • If not you better instruct clients to set-up application and configure your application/code with credentials they got.

Summarizing stuff:
You can create separate application for every client or instruct client to set-up application as part of install/set-up process for you application. Later you may use Client Side authentication flow to create generic code that will work for every client (this is possible with Server Side flow too, but will require some additional work and with JS-SDK FB.login it may be a drop-in functionality without any additional work).

相关问答

更多
  • 您可以像Kirk Larkin所说的那样做 - 使用客户端凭证流程。 以下代码在.NET中: var client = new TokenClient( BaseAddress + "/connect/token", "clientId", "clientSecret"); var result = client.RequestClientCredentialsAsync(scope: "my.api").Result; var ...
  • 您收到的错误告诉您,您在合作伙伴仪表板中为应用程序输入的应用程序URL的域与您在OAuth请求中提供的域不同。 确保它被列为本地主机,并且错误应该消失。 The error you're receiving is telling you that the domain of the application URL you entered for the app in the Partners dashboard differs from the one you're providing with your ...
  • 您的回叫网址不正确 - 它应该是http:// localhost:5000 / login / github / authorized Flask-Dance的文档表示,代码创建了一个带有两个视图“/ github”和“/ github / authorized”的蓝图“github”。 蓝图还配置了“/ login”的url_prefix,因此您的回调URL必须为http:// localhost:5000 / login / github / authorized 。 这段代码制定了一个蓝图,实现了在 ...
  • 它存在且名称为“无浏览器和输入约束设备的OAuth 2.0设备流程”,但尚未完全标准化,请参阅: https ://tools.ietf.org/html/draft-ietf-oauth-device-flow Google还以特定于供应商的方式实施了此流程avant-la-lettre: https : //developers.google.com/identity/protocols/OAuth2ForDevices It exists and has a name, "OAuth 2.0 Devi ...
  • 无论是页面标签还是画布,您都必须在https://developers.facebook.com/apps中标识网站的网址 我如何修复: 应用领域:megalopes.com(域名) 网站网址:/安全画布网址:/安全页面标签网址: https : //www.megalopes.com (子域名) Regardless of being page tab or canvas, you must identify the website Site URL in https://developers.faceb ...
  • 填写选项卡设置中的App Domain:和Site URL:字段。 这将有助于解决您的问题。 您必须确定重定向网址与您指定的网域相符。 Populate the App Domain: and Site URL: fields in the Tab Settings. That will help with your problem. And you have to be sure that the Redirect URL matches the domain you specify.
  • 您可以考虑客户端身份验证流程或使用带有OAuth对话框的 JS-SDK ,这样您可以轻松避免指定redirect_uri因为它可能由JS-SDK自动提供(或者您可以使用当前URL window.location ,如客户端文档中所示)侧认证流程)。 笔记: 虽然这可以帮助你避免使用redirect_uri实际问题更深一点...... redirect_uri使用将使这种流程难以实现,不仅因为无法预测它,而且由于要求redirect_uri应该位于App Domain中 ,因此使用JS-SDK也是如此。 因此 ...
  • 在您的应用程序/设置下的https://developers.facebook.com/上 ,验证您所使用的网址是否与您正在使用的重定向网址相匹配。 移动实例是否从不同的URL运行? 如果这没有用,请包含您的代码。 On https://developers.facebook.com/ under your app/settings verify that the url you have matches the redirect url you are using. Is the mobile insta ...
  • 所以看起来在重定向URI中有数字会导致它无效。 So it appears that having numbers in the redirect URI causes it to be invalid.
  • 您需要在“canvas url”中放置的URL可能是localhost:8080(应用程序URL由“名称空间”决定,并且始终为http://apps.facebook.com/namespace (这必须是唯一的你的应用。) 您需要为http://developers.facebook.com/docs/reference/dialogs/feed/指定重定向URI