在mysite的/代码/ 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;
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个包含页面themename/templates/Includes/ContactForm.ss
<form $FormAttributes id="contactform" action="$Link/Connectors" method="post" class="validateform AjaxForm">
<% loop $Fields %>
$Field
<% end_loop %>
$Actions.dataFieldByName(action_doSubmit)
// I want this function to print Hello World but it doesn't
$TestFunction
</form>
Run Code Online (Sandbox Code Playgroud)
这工作正常,直到我想从模板中的同一个控制器运行另一个函数.
通常我只是创建一个公共函数并在模板中调用它 - 但这不起作用.
如何从自定义表单模板中访问函数?
我试图访问它,如各种方法$Top.TestFunction,$TestFunction()并$Parent.TestFunction
谢谢 - 阿什
这是一个范围问题.当Controller渲染模板时,将功能放在控制器中可以正常工作.在您的情况下,表单正在呈现模板,您必须$TestFunction使用customize()告诉您的表单何时应该使用它,例如在返回时:
return $form->customise(array(
'TestFunction' => $this->TestFunction()
));
Run Code Online (Sandbox Code Playgroud)