Zend_Form 手动设置和验证字段值

发布于 2025-01-07 11:23:44 字数 810 浏览 2 评论 0原文

我有一个带有下拉字段的 Zend_Form。 当用户在 url 中设置一个值时,应在此下拉列表中选择该值作为默认值。

所以我现在要做的是:

$parlang = $this->getRequest()->getParam('lang');
if($parlang){
    $this->view->filterForm->getElement('ddLanguage')->setValue($parlang);
}

if ($this->getRequest()->isPost()) {
        if($this->view->filterForm->isValid($_POST)){
...
...
...

不,我想检查变量的值是否是下拉列表的有效值?我如何结合表单验证来检查这一点。是的,我可以根据数组检查变量,但这似乎是“与框架作斗争”。

那么 Zend 是如何做到这一点的呢?

编辑: 对于所有感兴趣的人,我的最终解决方案是:

$parlang = $this->getRequest()->getParam('lang');
if($parlang){
    $ddLanguage = $this->view->filterForm->ddLanguage;
    if($ddLanguage->isValid($parlang)){
        $ddLanguage->setValue($parlang);
        $language = $parlang;
    }
}

I have a Zend_Form with a dropdown field.
When the user sets a value in the url this one should be selected as default value in this dropdown.

So what i do at the moment is this:

$parlang = $this->getRequest()->getParam('lang');
if($parlang){
    $this->view->filterForm->getElement('ddLanguage')->setValue($parlang);
}

if ($this->getRequest()->isPost()) {
        if($this->view->filterForm->isValid($_POST)){
...
...
...

No i want to check if the value of the variable is even a valid value for the dropdown? How can i check this in coorporation with the form validation. Yes i can check the variable against a array or so but this seems to be "fighting against the framework".

So what is the Zend way to do such a thing?

Edit:
My final solution for all who are interested, is:

$parlang = $this->getRequest()->getParam('lang');
if($parlang){
    $ddLanguage = $this->view->filterForm->ddLanguage;
    if($ddLanguage->isValid($parlang)){
        $ddLanguage->setValue($parlang);
        $language = $parlang;
    }
}

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

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

发布评论

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

评论(2

感性 2025-01-14 11:23:44

我进行了快速测试,看起来您可以使用的一种方法是 Zend_Form_Element_Select::getMultiOption() 来检查选择值中是否存在该语言。

<?php

$parlang = $this->getRequest()->getParam('lang');

if ($parlang) {
    $el = $this->view->filterForm->getElement('ddLanguage');

    // attempt to get the option
    // Returns null if no such option exists, otherwise returns a
    // string with the display value for the option
    if ($el->getMultiOption($parlang) !== null) {
        $el->setValue($parlang);
    }
}

I ran a quick test and it looks like one method you can use is Zend_Form_Element_Select::getMultiOption() to check if the language exists in the select values.

<?php

$parlang = $this->getRequest()->getParam('lang');

if ($parlang) {
    $el = $this->view->filterForm->getElement('ddLanguage');

    // attempt to get the option
    // Returns null if no such option exists, otherwise returns a
    // string with the display value for the option
    if ($el->getMultiOption($parlang) !== null) {
        $el->setValue($parlang);
    }
}
遥远的绿洲 2025-01-14 11:23:44

如果您的多选元素包含国家/地区列表,我将根据 URL 中的值在您的元素值中填充默认值。

为此,您可以创建一个自定义 Zend_Form_Element,如下所示:

class My_Form_Element_SelectCountry extends Zend_Form_Element_Select
{
    protected $_translatorDisabled = true;

    public function init()
    {
        $locale = Zend_Registry::get('Zend_Locale');

        if (!$locale) {
            throw new Exception('No locale set in registry');
        }

        $countries = Zend_Locale::getTranslationList('territory', $locale, 2);
        unset($countries['ZZ']);

        // fetch lang parameter and set US if there is no param
        $request = Zend_Controller_Front::getInstance()->getRequest();
        $lang = $request->getParam('lang', 'US');

        // sort your country list
        $oldLocale = setlocale(LC_COLLATE, '0');
        setlocale(LC_COLLATE, 'en_US');
        asort($countries, SORT_LOCALE_STRING);
        setlocale(LC_COLLATE, $oldLocale);

        // check weither the lang parameter is valid or not and add it to the list
        if (isset($countries[$lang])) {
            $paramLang = array($lang => $countries[$lang]);
            $countries = array_merge($paramLang, $countries);
        }        

    $this->setMultiOptions($countries);
}  

}

您可以从这个自定义表单中得到这个想法。
如果您想要做的不是由国家/地区列表填充的多选字段,而是由语言列表填充,那么逻辑是相同的,您只需更改对静态方法 Zend_Locale:: 的调用即可getTranslationList() 并获取您需要的任何信息。

还有一件事,如果您只想在 Multiselect 元素中使用单个元素,那么请选择 Zend_Form_Element_Hidden

这是很多“如果”,但我无法从您的问题中完全理解您的多选元素的外观。

现在让我们看一下验证方面,当您使用 Multiselect 元素时,Zend_Framework 会自动添加一个 InArray 验证器,这意味着您无需执行任何操作即可检查发送的数据是否正确是否正确。 isValid 将为您做到这一点。

无论用户使用默认参数,一切都会好起来,还是修改/删除此参数,默认参数(在本例中为 en_US,请参阅上面的代码)将被设置为“多选”字段的默认值。

要回答你的最后一个问题,不,检查用户设置的变量并将其与数组进行比较(例如来自 getTranslationList())并不违反框架。我想说这甚至是推荐的做事方式。

If your Multiselect element contains a list of country, I would just populate a default in your element value according to the one in the URL.

In order to do so, you could create a custom Zend_Form_Element as follow:

class My_Form_Element_SelectCountry extends Zend_Form_Element_Select
{
    protected $_translatorDisabled = true;

    public function init()
    {
        $locale = Zend_Registry::get('Zend_Locale');

        if (!$locale) {
            throw new Exception('No locale set in registry');
        }

        $countries = Zend_Locale::getTranslationList('territory', $locale, 2);
        unset($countries['ZZ']);

        // fetch lang parameter and set US if there is no param
        $request = Zend_Controller_Front::getInstance()->getRequest();
        $lang = $request->getParam('lang', 'US');

        // sort your country list
        $oldLocale = setlocale(LC_COLLATE, '0');
        setlocale(LC_COLLATE, 'en_US');
        asort($countries, SORT_LOCALE_STRING);
        setlocale(LC_COLLATE, $oldLocale);

        // check weither the lang parameter is valid or not and add it to the list
        if (isset($countries[$lang])) {
            $paramLang = array($lang => $countries[$lang]);
            $countries = array_merge($paramLang, $countries);
        }        

    $this->setMultiOptions($countries);
}  

}

You get the idea from this custom form.
If what you're trying to do isn't a Multiselect field filled by a country list but a list of language instead, then the logic is the same, you just need to change the call to the static method Zend_Locale::getTranslationList()and grab whatever information you need.

One more thing, if you just want a single element in your Multiselect element, then go for a Zend_Form_Element_Hidden.

It's a lot of "if" but I can't understand how looks like your Multiselect element exactly from your question.

Now let's take a look on the validation side, when you're using a Multiselect element, Zend_Framework automatically adds an InArray validator, which means that you don't have anything to do to check weither the data sent are correct or not. isValid is going to do it for you.

Weither a user let the default parameter and everything will be fine, or he modifies/deletes this parameter and the default parameter (en_US in this case, see code above) is going to be set as a default value for the Multiselect field.

To answer your last question, no it's not against the framework to check a variable set by a user and compare it with an array (from getTranslationList()for example). I would say it's even the recommended way to do things.

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