我以某种形式建立了选择性的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.
请帮助我解决这一错误!