unclecheese / zen-fields

为 SilverStripe FieldLists 提供语法糖

安装数量: 19,557

依赖项: 1

建议者: 0

安全: 0

星标: 29

关注者: 7

分支: 4

开放问题: 6

类型:silverstripe-module

1.0.2 2014-05-19 21:47 UTC

This package is auto-updated.

Last update: 2024-09-22 14:10:42 UTC


README

为你的 SilverStripe FieldLists 提供语法糖。

基本用法

<?php
$fields
  ->text("FirstName")
  ->numeric("Age");

语法糖为

<?php
$fields->addFieldToTab("Root.Main", TextField::create("FirstName", "First Name"),"Content");
$fields->addFieldToTab("Root.Main", TextField::create("Age", "Age"),"Content");

在带有 TabSets 的 FieldLists 中,除非有其他指定,否则默认标签为 "Root.Main"。如果没有提供标签,则自动从字段名生成。通配字段方法是指任何从 FormField 派生出来的子类,移除了 "Field" 后缀,并且首字母小写。示例

  • text() -> TextField
  • currency() -> CurrencyField
  • treeDropdown() -> TreeDropdownField

指定标签

<?php
$fields
  ->tab("PersonalInfo")
    ->text("FirstName")
    ->numeric("Age")
  ->tab("Qualifications")
    ->grid("Qualifications","Qualifications", $this->Qualifications(), GridFieldConfig_RecordEditor::create());
    

实例化后修改字段

为了可链式操作,每个方法都返回 FieldList,因此为了访问 FormField 对象,你必须使用 configure() 访问器,然后使用 end() 返回到 FieldList 对象。

<?php
$fields
  ->dropdown("PickOne")
    ->configure()
      ->setSource(array('1' => 'One', '2' => 'Two'))
      ->setEmptyString("-- None --")
    ->end()
  ->text("Title");

语法糖为

<?php
$fields->addFieldToTab("Root.Main", DropdownField::create("PickOne","Pick One")
  ->setSource(array('1' => 'One', '2' => 'Two'))
  ->setEmptyString("-- None --")
);
$fields->addFieldToTab("Root.Main", TextField::create("Title"));

分组字段

你可以使用 group() 方法实例化一个 FieldGroup。

<?php
$fields
  ->text("Name")
  ->group()
    ->text("Address")
    ->text("City")
    ->text("PostalCode")
  ->end();

语法糖为

<?php
$fields->addFieldToTab("Root.Main", TextField::create("Name"));
$fields->addFieldToTab("Root.Main", FieldGroup::create(
  TextField::create("Address"),
  TextField::create("City")
  TextField::create("PostalCode")
));

额外功能

有一些快捷方法用于添加常见的字段配置。

<?php
$fields
  ->imageUpload("MyImage")
  ->hasManyGrid("RelatedObjects","Related objects", $this->RelatedObjects())
    ->configure()->addDragAndDrop("Sort")->end();