Zend框架动作助手还是其他什么?

时间:2012-01-13 18:20:39

标签: zend-framework zend-form

我的zend框架项目中需要链式选择框:Countries-> Regions-> Provinces-> Towns。

我正在使用zend表单并打算重新提交以在其中一个更改时重新加载链接选择框的内容。我在PHPunit测试中编写了一些代码来模拟我在控制器中需要的内容。 我将要求在我的网站上以不同的形式使用这种区域结构,并计划使用AJAX进行增强。

我不想复制这段代码,所以我应该在哪里存储它以及如何构造它以便我可以重用它的功能。我想也许是一个动作助手?

public function testCanGetRegionalStructureFromUser() {
        $user = new \Entities\User;
        $user = $this->em->getRepository('Entities\User')->findOneByEmail('testuser@yahoo.com');

        $town = new \Entities\Town;
        $town = $user->getTowns_id();

        // if user has not town input, then use the default 
        if (is_null($town)) {
            $config = Zend_Registry::get('config');
            $defaulttownid = $config->towns->defaultid;
            $town = $this->em->getRepository('Entities\Town')->find($defaulttownid);
        }

        // get the town id
        $townid = $town->getId();

        //get the province
        $province = $town->getProvinces_id();
        $provinceid = $province->getId();

        //get the region
        $region = $province->getRegions_id();
        $regionid = $region->getId();

        //get the country
        $country = $region->getCountries_id();
        $countryid = $country->getId();

        $countrylist = $this->em->getRepository('Entities\country')->findActiveCountries();
        $regionlist = $this->em->getRepository('Entities\Region')->findActiveRegions($countryid);
        $provincelist = $this->em->getRepository('Entities\Province')->findActiveProvinces($regionid);
        $townlist = $this->em->getRepository('Entities\Town')->findActiveTowns($provinceid);
    }

countrylist,regionlist等已准备好作为将用于填充选择框的选项注入我的表单。

1 个答案:

答案 0 :(得分:1)

我认为对于这种情况,创建复合Zend_Form_Element最有意义。

这样,您只需几行代码就可以轻松地将元素添加到表单中,您可以在元素中内置验证器,这样您就不会重复该逻辑,并且您可以使用自己的装饰器轻松控制选择的布局,只需要更改一个文件以反映所有表单的更改。

对于我的一个项目,我创建了一个复合元素,它有一个纯文本框,使用自动完成功能在用户输入时实时搜索客户。从自动完成列表中选择客户后,将进行ajax调用以获取该客户拥有的属性列表,并使用新列表更新下拉框。

该元素提供对数据值(customer,property)的访问,并且装饰器呈现组中的各个元素,并设置必要的事件处理程序和ajax调用(大多数代码位于外部.js文件中。

Creating and Rendering Composite Elements
Similar to the above reference, by Matthew Weier O'Phinney
Video: Creating composite elements

相关问题