【问题标题】:Silverstripe - 2 forms, 1 form actionSilverstripe - 2 个表格,1 个表格动作
【发布时间】:2017-06-13 13:35:10
【问题描述】:

我在同一个控制器上有 2 个表单,但我无法让第 2 个表单从同一个表单操作写入数据库。第一种形式写没有问题。我认为我的问题是让第二种形式付诸行动。 我将如何传递要执行的第二个表单数据?我可以以某种方式从一个操作中调用这两个函数吗?

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("");
 }

【问题讨论】:

  • 请澄清您的问题。我在您的代码中看到两个单独的操作,空的“RequiredFields”可能会导致问题。
  • 在开始工作时,我暂时将必填字段留空。我想调用这两种操作,但只是从一种形式。我应该将这两个动作合二为一吗?
  • 基本上:是的。如果您想通过单击一个按钮来完成这两项操作,请将其组合起来。
  • 那将是我的下一个问题。我如何将数据从第二种形式传递到第一种形式使用的相同函数?
  • 你为什么不从第一个动作中调用第二个动作,例如:$submission->write(); self::Action2($data, $form);,因为你似乎在传递相同的数据并调用相同的重定向?我不确定你想要实现什么,但看起来你可能需要一些逻辑,或者如果你总是调用这两个操作,它会在每次提交表单时创建一个客户

标签: php forms controller silverstripe


【解决方案1】:

目前还不清楚您要达到什么目的,但总的来说,您可以将所有数据以一种形式传递并分步处理。

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);
}

【讨论】:

    猜你喜欢
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多