问题描述
||
我已经在Page.php文件中定义了searchform
函数AdvancedSearchForm(){
$ searchText = isset($ this-> Query)吗? $ this-> Query:\'搜索\';
$ searchText2 = isset($ this-> Subquery)? $ this-> Subquery:\'0 \';
$ searchText3 = isset($ this-> documentType)吗? $ this-> documentType:\'全部\';
$searchSections = DataObject::get(\'SearchSection\');
$ss = array();
foreach ($searchSections as $section) {
$ss[$section->ID] = $section->Name;
}
$fileTypes = DataObject::get(\'FileType\');
$ft = array();
foreach ($fileTypes as $type) {
$ft[$type->ID] = $type->Name;
}
$ft[0] = \'All\';
ksort($ft);
$fields = new FieldSet(
new TextField(\'Query\',\'Keywords\',$searchText),new DropdownField(\'Subquery\',\'\',$ss,$searchText2),new DropdownField(\'documentType\',$ft,$searchText3)
);
$actions = new FieldSet(
new FormAction(\'AdvancedSearchResults\',\'Search\')
);
$form = new Form($this->owner,\'AdvancedSearchForm\',$fields,$actions);
$form->setFormMethod(\'GET\');
$form->setTemplate(\'Includes/SearchForm\');
$form->disableSecurityToken();
return $form;
}
并且我的所有页面都从“页面”扩展而来,并且搜索表单位于标题中,因此出现在所有页面上。但是,当用户在“安全性/登录”页面上并尝试搜索某些内容时,他会收到以下错误消息
The action \'AdvancedSearchForm\' does not exist in class Security
我认为这是因为“安全”页面未从“页面”扩展而来。如何设置搜索依据,使其可以在包括系统页面(如安全性登录页面)在内的任何页面上使用?
解决方法
因为表格未传递到所需的控制器(〜Page_Controller),所以出现错误。
定义Form对象时,应测试$ this-> owner并查看它是否为Page_Controller的后代。如果没有,则可以使用类似的方法,并将$ controller用作Form对象创建中的第一个变量。
$sitetree_object = SiteTree::get_by_link(\'some-page-url\');
$controller = ModelAsController::controller_for($sitetree_object);