Zend_Form元素基于其他属性的选择?

时间:2013-11-25 13:17:08

标签: jquery ajax zend-framework

有没有办法根据同一表单上select元素中选择的内容更改表单上select元素的值(多选项)?

例如:

Select 1: Country -> Australia

Select 2: City -> Must now populate with Brisbane, Sydney, New South Wales and Melbourne.

如何做到这一点,我正在使用模型来获取国家/地区名称和城市。

我想避免在每个案例中编写大量代码,即。生成一个新表单并通过ajax和类似的东西发送。什么是可接受和最优雅的解决方案?

2 个答案:

答案 0 :(得分:1)

最简单的方法是禁用InArrayValidator:

$cityElement->setRegisterInArrayValidator(false);

这意味着您现在可以执行以下操作:

$('#country').change(function(val) {
    $('#city').load('/yourcontroller/getcities/?country=' + val);
});

然后您的行动可能如下所示:

public function getcitiesAction() 
{
    // disable the layout and view
    $this->_helper->layout()->disableLayout(); 
    $this->_helper->viewRenderer->setNoRender(true);

    $cities = $this->getCities($this->_request->getQuery('country'));
    foreach ($cities as $city) {
        echo '<option value="'.$city->id.'">'.$city->name.'</option>';
    }
}

或者,您可以通过扩展Zend_Form_Element_Select并覆盖isValid($value, $context = null)来创建自己的Zend_Fom_Element。这样,一旦通过$content获得国家/地区,您就可以填充多重选项。这将要求您在国家/地区更改后(部分)更新表单。

答案 1 :(得分:-1)

我不知道你是否可以在不使用AJAX或w webmethod的情况下做到这一点。你知道怎么做吗?

相关问题