简体   繁体   English

是否可以在 Symfony 2 中创建自定义表单字段属性?

[英]Is it possible to create custom form field attributes in Symfony 2?

In Symfony2 form component is it possible to create custom attributes?在 Symfony2 表单组件中是否可以创建自定义属性?

The reason why I ask is because I'm working on a certain edge case where read_only will not be sufficient.我问的原因是因为我正在处理 read_only 不够的某个极端情况。

Here is the scenario: I need to bind data based on the outcome of some external logic parsing.这是场景:我需要根据一些外部逻辑解析的结果来绑定数据。 This is crucial because I may have fields that are disabled by default but based on the external logic the fields may be activated.这很重要,因为我可能有默认禁用的字段,但根据外部逻辑,这些字段可能会被激活。 I cannot use client scripting to produce this outcome, it has to be disabled in the form attribute.我不能使用客户端脚本来产生这个结果,它必须在表单属性中被禁用。

If I start with the field as read_only, it will be disabled, but I will never be able to bind data to it.如果我以只读字段开头,它将被禁用,但我永远无法将数据绑定到它。 So given the outcome of my aforementioned external logic, I will not be able to use read_only.因此,鉴于上述外部逻辑的结果,我将无法使用 read_only。 So this leaves me with no other option but using a different attribute which will make the field disabled.所以这让我别无选择,只能使用不同的属性来禁用该字段。

Is it possible to create a custom attribute to produced this disabled effect?是否可以创建自定义属性来产生这种禁用效果?

I'm not sure I've understood your question correctly;我不确定我是否正确理解了您的问题; do you want to add arbitrary attributes to your form input tags?你想为你的表单输入标签添加任意属性吗? For example:例如:

<input type="text" name="myInput" myAttr="myValue" />

If this is what you want to do, then this is possible, like so:如果这是您想要做的,那么这是可能的,如下所示:

$form = $this->createFormBuilder($someObj)
        ->add('myInput', 'text', array(
             'attr' => array('myAttr' => 'myValue')
        )
        ->getForm();

The documentation is here:文档在这里:

http://symfony.com/doc/2.0/reference/forms/types/field.html http://symfony.com/doc/2.0/reference/forms/types/field.html

Hard to tell exactly what you're looking to do, but sounds like you want dynamically generated forms based on some event, which is described here:很难确切地说出您要做什么,但听起来您希望根据某些事件动态生成 forms,如下所述:

http://symfony.com/doc/2.0/cookbook/form/dynamic_form_generation.html http://symfony.com/doc/2.0/cookbook/form/dynamic_form_generation.html

Your limitation is not Symfony, your limitation is HTML and HTTP.您的限制不是 Symfony,您的限制是 HTML 和 HTTP。 Unfortunately, once the HTTP request is fulfilled, once that data is sent to the browser, there is nothing a server can do to change what is rendered (well, almost nothing, there is always Skynet).不幸的是,一旦 HTTP 请求得到满足,一旦数据被发送到浏览器,服务器就无法改变渲染的内容(嗯,几乎没有,总是有天网)。 The only option is JavaScript (and that can do a lot if they're not running Lynx).唯一的选择是 JavaScript(如果他们不运行 Lynx,这可以做很多事情)。

I saw your question on Google Groups and based on the combination of both of them, I can tell you that you only have two options.我在 Google Groups 上看到了您的问题,根据两者的结合,我可以告诉您,您只有两个选择。

  • You can make the option appear as a response to the first response you have from the browser.您可以使该选项显示为对您从浏览器获得的第一个响应的响应。
  • You can use JavaScript and then handle any failures on the server side.您可以使用 JavaScript 然后处理服务器端的任何故障。

Your best bet?你最好的选择? I think the users will appreciate the JavaScript option.我认为用户会喜欢 JavaScript 选项。 It is good policy to validate user information server-side anyway.无论如何,在服务器端验证用户信息是一个很好的策略。 Obviously let the user know as soon as possible by validating with JavaScript, but you'll need to be checking their input on the server anyway.显然,通过 JavaScript 验证尽快让用户知道,但无论如何您都需要检查他们在服务器上的输入。

By the way, to disable a form field in Symfony, step-by-step instructions are here .顺便说一句,要禁用 Symfony 中的表单域,这里有分步说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM