0

我正在尝试将数据加载到表单中。我正在使用 Uncle Cheese 的 bootstrap-forms 模块。据我了解,我通常会调用它,$form->loadDataFrom(Customer::get()->byID(4));但引导模块以不同的方式构造表单。

//CREATE CUSTOMER FORM
public function CustomerForm() {
    return BootstrapForm::create(   
        $this,
        "CustomerForm",
        FieldList::create(
            TextField::create("Name","Name")
            TextField::create("Surname","Surname"),

            FieldList::create(
                FormAction::create("AddCustomer","Add customer")
                    ->setStyle("success")
            ),
            RequiredFields::create(array("Name","Surname","Tel1", "Address")),

            /// I think the load data method should go here but i keep getting errors
            BootstrapForm::loadDataFrom(Customer::get()->byID(4)
        )
    )
}

任何帮助将不胜感激。

4

1 回答 1

1

您需要在表单创建之外调用 loadDataFrom() 函数以传入 DataObject 以填充字段。

注意:在下面的示例中,传递给 BootstrapForm::create() 的每个参数都拆分为变量。如果您以后需要更改表单参数,这往往会使代码更具可读性和简单性。

我发现这是一种很好的做法,而不是试图将所有内容都塞进一个方法调用中。

一旦数据被加载到表单中,我们就返回表单对象:

return $form;

这使得表单对视图层可用。例如,在 SilverStripe *.ss 模板中,您可以通过在标记中放置 $CustomerForm 占位符来包含此表单。

希望能帮助到你。

//CREATE CUSTOMER FORM
public function CustomerForm(){

    $fields = FieldList::create(
        TextField::create("Name","Name"),
        TextField::create("Surname","Surname")
    );

    $actions = FieldList::create(
        FormAction::create("AddCustomer","Add customer")
        ->setStyle("success")
    );

    $validator = RequiredFields::create(array("Name","Surname","Tel1", "Address"));

    $form = BootstrapForm::create($this, "CustomerForm", $fields, $actions, $validator);

    //Load your data into the form here.
    $form->loadDataFrom(Customer::get()->byID(4));

    return $form;
}
于 2013-10-10T20:53:55.920 回答