如何在CMS中创建所需的字段

时间:2017-08-14 11:27:44

标签: php silverstripe

我尝试在CMS中创建所需的字段:

class Documents extends DataObject {

    private static $db = array(
        'DocType' => 'Text',
        'DocTitle' => 'Text',
        'DocNumber' => 'Text'
    );

    public function getCMSFields() {
        $fields = parent::getCMSFields();   
        $fields = FieldList::create(TabSet::create('Root'));
        $fields->addFieldsToTab('Root.Main', array(
            DropdownField::create('DocType','Document Type'),             
            DropdownField::create('DocStatus','Document Status'),
            TextField::create('DocNumber','Document Number'),
            ...

            RequiredFields::create(array('DocType','DocTitle','DocNumber'));
        ));

        return $fields;
    }

但在我的SilverStripe错误日志中,我得到以下内容:

  

“Uncaught Exeption:RequiredFields上不存在方法'getname',或者该方法不公开”。

如何在SilverStripe CMS中创建所需的字段?

1 个答案:

答案 0 :(得分:6)

在CMS中,我们可以通过声明getCMSValidator函数并返回RequiredFields来声明必填字段:

public function getCMSValidator()
{
    return RequiredFields::create(
        'DocType',
        'DocTitle',
        'DocNumber'
    );
}