我已经坚持了好一阵子了,我看不出我要去哪里。
我制作了一个扩展UserDefinedForm的类,因此我可以在页面上也包含UserDefinedForm。表单加载到页面上,但是不像在模板文件上说$ Form那样简单,相反,我必须制作并调用以下函数:
public function getUserForm() {
$page = UserDefinedForm::get()->byID($this->ID);
$controller = UserDefinedFormController::create($page);
return $controller->Form();
}
因此,当我在模板上调用该函数时,它会显示表单,但是没有应用任何字段规则,并且在提交表单时,它将带我到空白页,并在URL上附加了“ / finished”:“ { {3}}”。
有人可以在这里帮助我吗,将不胜感激。 我将代码放到下面。
扩展UserDefinedForm的类:
<?php
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\FileHandleField;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Assets\Image;
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use SilverStripe\Forms\DropdownField;
use SilverStripe\UserForms\Model\UserDefinedForm;
use SilverStripe\UserForms\Control\UserDefinedFormController;
use SilverStripe\UserForms\Model\EditableCustomRule;
class Package extends UserDefinedForm {
private static $db = [
'Date' => 'Text',
'Location' => 'Text',
'Availability' => 'Enum(array("Available","Hidden","Sold Out"))',
'Extras' => 'HTMLText',
'NeedTo' => 'HTMLText',
'Price' => 'Text'
];
private static $has_one =[
'Photo' => Image::class,
];
private static $has_many =[
'FileAttachments' => 'PackageFile'
];
private static $many_many = [
'SacredTexts' => 'ImportantText'
];
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main', Injector::inst()->create(FileHandleField::class, 'Photo'));
$fields->addFieldToTab('Root.Main', new TextField('Date', 'Date'));
$fields->addFieldToTab('Root.Main', new TextField('Location', 'Location'));
$fields->addFieldToTab('Root.Main', new TextField('Price', 'Price'));
$fields->addFieldToTab('Root.Main', new HTMLEditorField('Extras', 'Extras'));
$fields->addFieldToTab('Root.Main', new HTMLEditorField('NeedTo', 'What you need to know'));
$fields->addFieldToTab('Root.Main', new DropdownField('Availability', 'Availability', singleton('Package')->dbObject('Availability')->enumValues()));
$fields->addFieldToTab('Root.Main', new GridField('SacredTexts', 'Important Texts', $this->SacredTexts(), GridFieldConfig_RecordEditor::create()),'Date');
$fields->addFieldToTab('Root.Main', new GridField('FileAttachments', 'File Attachments', $this->FileAttachments(), GridFieldConfig_RecordEditor::create()),'Content');
return $fields;
}
/* Look up SS4 docs on SS Sitetree URL parse function and what needs to be namespaced */
function onBeforeWrite () {
parent::onBeforeWrite ();
if($this->Name){
$this->Slug = str_replace(' ','-', strtolower($this->Name));;
}
}
public function ShortContent( $word_limit = 20 ) {
$NoHTML = htmlspecialchars_decode(strip_tags($this->NewsText),ENT_QUOTES);
$words = explode( ' ', $NoHTML );
return implode( ' ', array_slice( $words, 0, $word_limit ) );
}
public function ParentEvent(){
return $this->Parent()->URLSegment;
}
public function getUserForm() {
$page = UserDefinedForm::get()->byID($this->ID);
$controller = UserDefinedFormController::create($page);
return $controller->Form();
}
public function hasUserForm() {
if (count($this->getUserForm()->Fields()) > 1) {
return $this->getUserForm();
}
}
}
答案 0 :(得分:2)
我知道了。
结果是我扩展以覆盖的类控制器不起作用,而是引用了UserDefinedFormController。我通过从UserDefinedForm类复制以下函数来修复它:
public function getControllerName()
{
return UserDefinedFormController::class;
}
并将其从UserDefinedForm复制到扩展类中,然后将return语句重命名为该类的名称,例如:
public function getControllerName()
{
return PackageController::class;
}