php表格录入模板,php – Silverstripe从自定义表单模板内部访问功能

在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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值