Silverstripe - 2种形式,1种形式动作

时间:2017-01-27 19:07:30

标签: php forms controller silverstripe

我在同一个控制器上有两个表单,但我无法从第二个表单写入来自同一个表单操作的数据库。第一种形式没有问题。我认为我的问题是让第二种形式发挥作用。 我如何通过第二个表格数据进行操作?我能以某种方式从一个动作中调用这两个函数吗?

public function CustomerForm() {        
    $form = BootstrapForm::create(
    $this,
    __FUNCTION__,
    FieldList::create(
        TextField::create("FirstName","FirstName")),            
        FieldList::create(
        FormAction::create("Action1","Save")->setStyle("success")),
        RequiredFields::create(array("FirstName"))  
        )->setLayout("horizontal")->addWell();          
        return $form;
    }

public function HiddenForm() { 
    $form = BootstrapForm::create(
    $this,
    __FUNCTION__,
    FieldList::create(
        HiddenField::create("CustomersID","CustomersID", $this->urlParams['ID'])),
        FieldList::create(
        FormAction::create("Action2","Save")->setStyle("success")),
        RequiredFields::create(array(""))   
        )->setLayout("horizontal")->addWell(); 
        return $form;
    }   

public static function Action1($data, $form) {
    $form->sessionMessage('Update successful', 'success');
    $submission = Customer::get()->byID($data["ID"]);  
    $form->saveInto($submission);
    $submission->write();
    return Controller::curr()->redirect("");
}

 public static function Action2($data, $form) {
    $submission = new SecondTable();
    $form->sessionMessage('The customer has been created', 'success');
    $form->saveInto($submission);
    return Controller::curr()->redirect("");
 }

1 个答案:

答案 0 :(得分:0)

目前还不清楚您尝试实现的目标,但通常您可以在一个表单中传递所有数据并分步处理。

public function CustomerForm() {
  $form = BootstrapForm::create(
    $this,
    __FUNCTION__,
    FieldList::create(
        TextField::create("FirstName","FirstName")),

        // this is unclear what is the relation to the current page
        HiddenField::create("CustomersID","CustomersID", $this->urlParams['ID'])),

    FieldList::create(
        FormAction::create("doSave","Save")->setStyle("success")),
    RequiredFields::create(array("FirstName"))
  )->setLayout("horizontal")->addWell();

  return $form;
}


public function doSave($data, $form) {
    // split update in steps
    $this->updateCustomer($data, $form);
    $this->updateSubmission($data $form);
    $form->sessionMessage('The customer has been created', 'success');
    return Controller::curr()->redirect("");
}

protected function updateCustomer($data, $form) {
    $customer = Customer::get()->byID($data["ID"]);
    $form->saveInto($customer);
    $customer->write();
}

protected function updateSubmission($data, $form) {
    $submission = new SecondTable();
    $form->saveInto($submission);
}