2013-11-25 124 views
0

有没有一种方法可以根据在同一个表单上选择元素中选择的内容来更改表单上select元素的值(多选项)?基于其他属性选择的Zend_Form元素?

例如:

Select 1: Country -> Australia 

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

如何才能做到这一点,和我使用一个模型来获取国家名称和城市。

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

回答

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来创建自己的Zend_Fom_Element并覆盖isValid($value, $context = null)。通过这种方式,您可以通过$content获得一个国家/地区的多选项。这将要求您在国家变更后(部分)更新表格。

-1

我不知道你是否可以做到这一点,而不使用AJAX或w webmethod。你知道怎么做吗?

相关问题