English 中文(简体)
添加Zentd_Form_Element_ 通过控制器挑选
原标题:Problem adding options to Zend_Form_Element_Select through controller

我以某种形式建立了选择性的ELement。

    $parentId = $this -> createElement( select ,  parent_id );
    $parentId -> setLabel("Select a parent menu:")
                -> setRequired(true);
    $parentId->addMultiOption(0,  None );
    $this->addElement($parentId);

Its option have to loaded as per the value passed from the query String or URL. So, in my controller I fetched the values I required from the url and loaded extra item to the element, using following code

private function renderParentElement($menu_id, $parent = 0) {
    $mapper = $this -> mapper();
    $select = $mapper -> select();
    $select -> where("parent = ?", $parent)
            -> where("menu_id = ?", $menu_id);
    $menus = $mapper -> fetchAll($select);
    if($menus -> count() > 0) {
        foreach($menus as $menu) {
            $this -> form() -> getElement( parent_id ) -> addMultiOption($menu -> id, $menu -> label);
        }
    }
}

采用上述方法的行动方法是:

public function addAction()
{
    $menu = $this->_request->getParam( menu );

        $mapperMenu = new Application_Model_Mapper_Menu();
        $this -> view -> menu = $mapperMenu -> find($menu);

        if($this -> _request -> isPost() && $this -> form() -> isValid($_POST)) {
            $data = $this -> form() -> getValues();
            $menuItem = $this -> model();
            $menuItem -> setParent($data[ parent ]);
            $menuItem -> setMenu_id($data[ menu_id ]);
            $menuItem -> setLabel($data[ label ]);
            $menuItem -> setLink($data[ link ]);
            $menuItem -> setPage_id($data[ page_id ]);
            $this -> mapper() -> save($menuItem);

            $this -> _request -> setParam( menu , $data[ menu_id ]);
            $this -> _forward( index );
        }
        $this -> form() -> populate(array( menu_id  => $menu));
        $this -> renderParentElement($menu, 0);
        $this->view->form = $this -> form();
}

Now everything thing was working fine. The elements were loaded correctly and displaying correctly as well. But when I submitted it, the select box gives error as 1 is not found is the haystack, here 1 is the value of the item i selected, which is loaded from the controller.

请帮助我解决这一错误!

最佳回答

From what I see you have no values in your element before you call isValid

$this->renderParentElement($menu, 0);
// you have to set the options in the form before you validate it
if($this->_request->isPost() && $this->form()->isValid($_POST)) { ... }

请注意,您不必两次要求您的翻译方法,只是将其移至发言之前。

问题回答

当你提交一种表格时,泽德试图与你选择的盒子中的价值相对应。 这一错误告诉你,在选择中找不到价值1的选项。 你们需要做的是,在你打电话之前,重新播种你选择的箱子。





相关问题
Zend 邮件问题,涉及外国char子+ com子

泽斯德邮局在名称被定为具有外国性质(如“保”)和 com(”)的物品时,就放弃了一种例外(因为邮局(邮局)退回假)。 重新提出以下守则。

PHP Framework: Ebay Like Site

I am going to be builiding a site like ebay - with all the features of ebay. Please note my payment method is limited to paypal. What would be the best PHP framework to use to build this quickly, ...

Link to a specific step in onepage checkout

Is it possible to redirect the browser to nth step in the onepage checkout? If so, how would one go about doing it? I m working on a payment module and have a sort of "cancel" action that i would ...

Tagging with Zend Framework

I m trying to create the same solutions as below, but using a simple MySQL query (instead of the static version used below where the words/tags are implemented in the code). The name of the MySQL ...

dynamicaly adding textboxes to zend_form

I hope that this is a quick question to answer. I am developing a form using Zend_Form, I have a number of Zend_Dojo_Form_Element_Textboxs to add to this form dynamically. These are added from rows ...

热门标签