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

发布于 2024-12-26 11:29:34 字数 1885 浏览 2 评论 0原文

我的 zend 框架项目需要链接选择框:国家->地区->省->城镇。

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

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

public function testCanGetRegionalStructureFromUser() {
        $user = new \Entities\User;
        $user = $this->em->getRepository('Entities\User')->findOneByEmail('[email protected]');

        $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);
    }

国家列表、区域列表等已准备好作为选项注入到我的表单中,这些选项将用于填充选择框。

I have a need for chained select boxes in my zend framework project: Countries->Regions->Provinces->Towns.

I am using zend form and intend to resubmit to reload the contents of the of the chained select boxes when one of them is changed. I have written some code in a PHPunit test to simulate what I will need in my controllers.
I will require to use this regional structure in quite a few different forms on my site and also plan to enhance with AJAX.

I don't want to be duplicating this code, so where should I store it and how should it be structured so that I can reuse its functionality. I thought perhaps an action helper?

public function testCanGetRegionalStructureFromUser() {
        $user = new \Entities\User;
        $user = $this->em->getRepository('Entities\User')->findOneByEmail('[email protected]');

        $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 etc. are ready to be injected into my form as options that will be used to populate the select boxes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情丝乱 2025-01-02 11:29:34

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

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

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

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

创建和渲染复合元素
与上述参考类似,作者:Matthew Weier O'Phinney
视频:创建复合元素

I think that for this case, creating a composite Zend_Form_Element makes the most sense.

This way, you can easily add the element to your forms with only a few lines of code, you can have validators built into the element so you aren't repeating that logic, and with your own decorator you can easily control the layout of the selections and only have to change one file to reflect the changes to all forms.

For one of my projects I created a composite element that had a plain text box that used autocomplete to search customers in real time as the user types. Once a customer is selected from the autocomplete list, an ajax call is made that fetches a list of properties owned by that customer and a dropdown box is updated with the new list.

The element provides access to the data values (customer, property) and the decorator renders the individual elements in a group, and set up the necessary event handlers and ajax calls (most of that code is in an external .js file.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文