如何在ZF2表单中动态设置valueOptions

时间:2016-01-18 14:31:56

标签: php forms zend-framework2

我正在尝试动态设置表单中的标题值。我的标题字段代码:

 $title->setValueOptions(array(
        "mr" => $trans->get("Reservation_passenger_type_mr"),
        "mrs" => $trans->get("Reservation_passenger_type_mrs"),
        "chld" => $trans->get("Reservation_passenger_type_child"),
    ));

然后我使用以下代码显示x次,具体取决于由成人和儿童组成的numberOfPassengers

$collection = new Collection('persons');        
$collection->setCount($this->numberOfPassengers); 
$this->add($collection);

如果Passangers的数量包括一些孩子,我想首先将chld标题显示为默认值:

$title->setValueOptions(array(
    "chld" => $trans->get("Reservation_passenger_type_child"),
    "mr" => $trans->get("Reservation_passenger_type_mr"),
    "mrs" => $trans->get("Reservation_passenger_type_mrs"),        
));

知道如何在代码中完成它吗?

1 个答案:

答案 0 :(得分:0)

您不必更改值选项的顺序,只需按如下方式设置字段的选定值:

if (/* collection includes some children */) {
    $title->setValue('chld');
}