简体   繁体   English

Symfony2动态表单字段

[英]Symfony2 dynamic form fields

I would like to create a form in Symfony2 which displays a set of radio buttons depending on the selected value in a combo-box belonging to the same form. 我想在Symfony2中创建一个表单,该表单根据属于同一表单的组合框中的选定值显示一组单选按钮。
I have not been able to find a suitable solution in Symfony2 that would allow me to have this functionality. 我无法在Symfony2中找到合适的解决方案来让我拥有此功能。
Is there any way to use the Symfony2 framework at least for part of the implementation or should I generate the form manually? 是否至少在部分实现过程中可以使用Symfony2框架,还是应该手动生成表单?

What you're looking for is AJAX. 您正在寻找的是AJAX。 This can be implemented with straight javascript or jQuery (jQuery is much cleaner and easier to use). 可以使用直接的javascript或jQuery来实现(jQuery更加简洁易用)。

Here's a jQuery stub for you to get an idea of what has to happen: 这是一个jQuery存根,让您了解发生了什么:

<script language="javascript">
    $('#mySelect').change(function() {
        $.post('backgroundScript.php', {
            val: $(this).val()
        }, function(data) {
            $('#radioDiv').html(data);
        });
    });
</script>

If you're not familiar with AJAX & jQuery, I'll explain what's going on. 如果您不熟悉AJAX和jQuery,我将解释发生了什么。 $('#mySelect') is almost the equivalent of javascript's getElementById() function. $('#mySelect')几乎等同于javascript的getElementById()函数。 They are NOT the same functionality (jQuery uses the same notations as css to find the elements you're looking for), but this will return the element with mySelect as its id attribute. 他们是相同的功能(jQuery使用相同的标记为CSS来找到你要找的元素),但这将返回元素mySelect作为其id属性。

.change() sets the onChange event handler to the function() defined within the brackets. .change()将onChange事件处理function()为方括号内定义的function() $.post is jQuery shorthand for performing an AJAX post (as opposed to a get ), that is sent to backgroundScript.php without a page load. $.post是jQuery的简写形式,用于执行AJAX post (与get相对),该post无需页面加载即可发送到backgroundScript.php When backgroundScript.php returns with a 200 status (ie "everything's OK, here's my output") it will perform the callback function function(data) . backgroundScript.php返回200状态(即“一切正常,这是我的输出”)时,它将执行回调函数function(data)

backgroundScript.php will receive val as a $_POST variable and do "something" to determine which radio buttons should be displayed, then will write output to the screen. backgroundScript.php将接收val作为$ _POST变量,并执行“操作”以确定应显示的单选按钮,然后将输出写入屏幕。

EDIT (more info): 编辑(更多信息):

backgroundScript.php should look something like this if you're determining the radio buttons based on a database. 如果要基于数据库确定单选按钮, backgroundScript.php应该看起来像这样。 Keep in mind that this is not written with security or consideration of deprecated functions in mind (eg mysql_* ), just functionality : 请记住,编写此文件时并未考虑安全性或考虑过时的功能(例如mysql_* ),而只是考虑了功能性

<?php
// backgroundScript.php
$masterVal = $_POST['val'];
$output = "";

$query = "SELECT bt.button_value, bt.button_label
    FROM button_table as bt
    WHERE bt.button_master_id = " . $masterVal;

$result = mysql_query($query);

while($row = mysql_fetch_array($result)) {
    $output .= "<input type='radio' name='radioGroup' value='" . 
        $row['button_value'] . "' /> " . $row['button_label'];
}

echo $output;

You could try using the symfony2 form builder, which permits to construct forms programmatically. 您可以尝试使用symfony2表单构建器,该构建器允许以编程方式构建表单。

This would be possible if you could determine the entity before the construction of the form... 如果您可以在构建表单之前确定实体,则可以这样做...

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

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