addmultioption array problem in Zend

▼魔方 西西 提交于 2019-12-01 14:05:16

Create Array like this

$myArray = array( 'NULL' => 'Select Month',
                     '1' => 'Jan',
                     '2' => 'Feb',
                     '3' => 'Mar',
                     '4' => 'Apr',
                     '5' => 'May',
                     '6' => 'Jun',
                     '7' => 'Jul',
                     '8' => 'Aug',
                     '9' => 'Sep',
                    '10' => 'Oct',
                    '11' => 'Nov',
                    '12' => 'Dec'
                 );

Create element like this:

$selectElement = $this->CreateElement('select', 'months');
$selectElement->setLabel('Label');
$selectElement->addMultiOptions( $myArray );

Be careful! If you add a NULL value like this to your Zend Form your $model that you will save will have the value string(4) "NULL"

//in the form

$task->addMultiOption('NULL','');

//as it appears in html

select id="fk_id_task_task" name="fk_id_task_task" option label="" value="NULL" /option

//model from form values

$values = $form->getValues(); $model->fromArray($values, true);

//dump the $model and you end up with string(4) "NULL"

$model->fk_id_task_task = string(4) "NULL"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!