在mysite / code / Connectors.php中,我在Page_Controller中创建了一个带有自定义模板的表单,这里是代码:
class Connectors_Controller extends Page_Controller {
private static $allowed_actions = array (
'TestForm',
'TestFunction'
);
public function TestFunction(){
return 'Hello World!';
}
public function TestForm(){
$fields = new FieldList(
new TextField('Test', 'Test')
);
$actions = new FieldList(
new FormAction('doSubmit', 'Submit')
);
$form = new Form($this, 'TestForm', $fields, $actions);
$form->setTemplate('ContactForm');
return $form;
}
}
我创建了一个包含页面themename / templates / Includes / ContactForm.ss
$Field
$Actions.dataFieldByName(action_doSubmit)
// I want this function to print Hello World but it doesn't
$TestFunction
这工作正常,直到我想从模板中的同一个控制器运行另一个函数.
通常我只是创建一个公共函数并在模板中调用它 – 但这不起作用.
如何从自定义表单模板中访问函数?
我尝试过各种访问它的方法,比如$Top.TestFunction,$TestFunction()和$Parent.TestFunction
谢谢
– 灰
解决方法:
这是一个范围问题.当Controller渲染模板时,将功能放在控制器中可以正常工作.在您的情况下,表单正在呈现模板,您必须告诉您的表单何时应该使用customise()替换$TestFunction,例如,返回时:
return $form->customise(array(
'TestFunction' => $this->TestFunction()
));
标签:php,silverstripe
来源: https://codeday.me/bug/20190528/1169254.html