php 如何填充zend表单选择元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3478787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to populate zend form select element?
提问by Hammad Tariq
Hello I have been searching for it from last couple of hours and have read every matching result google can give me but still can't get it to work.
您好,我最近几个小时一直在搜索它,并阅读了谷歌可以给我的所有匹配结果,但仍然无法让它工作。
I am creating a zend form select element through:
我正在通过以下方式创建一个 zend 表单选择元素:
this->addElement('select','my_select', array(
'label' => 'Currency', 'value' => 'blue',
'multiOptions' => array( 'red' => 'Rouge', 'blue' => 'Bleu', 'white' => 'Blanc', ), ) );
now I want to populate it through
现在我想填充它
$form->populate
from the controller, I have tried giving double dimensional array like
从控制器中,我尝试给出二维数组,例如
$vals = array("my_select" => array("US Dollar", "Pound Sterling"))
and then giving it in:
然后给它:
$form->populate($vals);
but this didnt worked and I am not sure if this will work, at the moment I am building my array like in array( 'red' => 'Rouge', 'blue' => 'Bleu', 'white' => 'Blanc')
the same class as zend form and then passsing it to the addElement multiOptions dynamically, as this guy suggests here:
http://zendguru.wordpress.com/2009/03/06/zend-framework-form-working-with-dropdownselect-list/that works but I would like to have it through populate and also if someone can suggest me how to select default value as well, I will be very grateful!
但这没有用,我不确定这是否可行,目前我正在构建我的数组,就像在array( 'red' => 'Rouge', 'blue' => 'Bleu', 'white' => 'Blanc')
与 zend 表单相同的类中一样,然后将它动态传递给 addElement multiOptions,正如这个人在这里建议的那样:
http:// zendguru.wordpress.com/2009/03/06/zend-framework-form-working-with-dropdownselect-list/有效,但我希望通过填充它,如果有人可以建议我如何选择默认值作为好吧,我将不胜感激!
Thanks,
谢谢,
回答by Jim W.
The populate method allows you to "populate" the form with default values IE:
populate 方法允许您使用默认值 IE 来“填充”表单:
$form->populate(array("my_select" => "red");
That would set the my_select to have "red" as being selected.
这会将 my_select 设置为“红色”被选中。
EDIT
编辑
As for the multi-dimensional array, I am not exactly sure what you are trying to do, do you want another select statement or to append those items? It seems like it should be another select element.
至于多维数组,我不确定您要做什么,您想要另一个选择语句还是附加这些项目?看起来它应该是另一个选择元素。
Looking more at it, what you want is to set that select up with values from the populate method, this is not an option. You can do something like this, however:
多看看它,您想要的是使用 populate 方法中的值设置选择,这不是一个选项。但是,您可以执行以下操作:
$currency = new Zend_Form_Element_Select('currency', array(
"label" => "Currency",
"required" => true,
));
$currency->addMultiOptions(array(
"US Dollar" => 1,
"Pound Sterling" => 2,
));
$form->addElements(array($currency));
$form->populate(array("currency" => "US Dollar"));
And that will add your currency select statement. And that should setup the drop down (the array of values could potentially come from the database etc) and then set US Dollar to be the default with the populate method.
这将添加您的货币选择语句。这应该设置下拉列表(值数组可能来自数据库等),然后使用 populate 方法将美元设置为默认值。
回答by LittleBigDev
For those like me who are about to waste hours of their precious time, BE CAREFUL ! If you added a filter to your select element, you must be 100% sure that the data passed to populate() contains the filteredvalues. If you don't, your dropdown box wont get any selected option ! In my case, I had a select element with these options :
对于像我这样即将浪费宝贵时间的人,请小心!如果向 select 元素添加了过滤器,则必须 100% 确定传递给 populate() 的数据包含过滤后的值。如果不这样做,您的下拉框将不会获得任何选定的选项!就我而言,我有一个带有以下选项的 select 元素:
'' => 'Choisir un pays',
'France' => 'France',
'Madagascar' => 'Madagascar',
'Maroc' => 'Maroc'
But I also passed the StringToLower filter to the element... If POST data contained the "Madagascar" value, "Madagascar" was passed in the populate() function and nothing was selected... But because of this StringToLower filter, I should have passed "madagascar" !
但是我也将 StringToLower 过滤器传递给了元素...如果 POST 数据包含“Madagascar”值,则在 populate() 函数中传递“Madagascar”并且没有选择任何内容......但是由于这个 StringToLower 过滤器,我应该已通过“马达加斯加”!
Long story short : be careful about the filters on your select element : populate() could be disturbed.
长话短说:小心选择元素上的过滤器:populate() 可能会受到干扰。
Additional edit : A checkbox with Int
filter and value="0"
will be checked if no data or any non-numeric string datais passed to populate()
for this box...
As this is a checkbox, I recommend to avoid the Int
filter, and use InArray
validator instead.
附加编辑:带有Int
过滤器的复选框,value="0"
如果没有数据或任何非数字字符串数据传递给populate()
此框,将被检查......由于这是一个复选框,我建议避免使用Int
过滤器,而使用InArray
验证器。