【问题标题】:Zend Form, can't take input values if there's a default value in elementZend Form,如果元素中有默认值,则无法获取输入值
【发布时间】:2012-11-17 11:53:28
【问题描述】:

我正在使用 Zend Framework 1.12,制作一个包含多个表单的页面。我在其中使用一个主表单和子表单。因此,我只有一个验证代码部分。 这些子表单指向数据库中的不同表。目的是,如果数据库中有关于该表单的行,表单应该从数据库中获取默认值,以便用户有机会更改该数据。如果数据库中没有一行,这种形式的输入将被插入到数据库中。 首先,我可以从 db 中获取值并将它们显示为表单元素的值。但是当我改变它并尝试使用

$form->getValues();

我无法访问页面中输入(或编辑)的值,我只是重新访问数据库中默认输入的值。这个表格应该可以随时编辑,我有多个表格来处理不同类型的数据,它们也可以做同样的事情。我一定做错了什么?任何想法?

(添加)这里是我的控制器代码的相关部分的摘要:

$masterform = new Application_Form_GeneralForm(); // a class which extends Zend_Form
$form1 = new Application_Form_SmallForm(); // a class which extends Zend_Form_Subform
$masterform->addSubform($form1, 'form1');
                         // so far, for form 1, no problem. My second form will be 
                         // added to the masterform after this first form is submitted,
                         // which works fine.

$form2 = new Application_Form_AnotherSmallForm(); // a class which extends Zend_Form_Subform

$request = $this->getRequest();
if ($request->isPost()){
    if ($generalform->isValid($request->getPost())) {
         $form2->loadValues(); // the part that form elements are filled with data 
                               // taken from db, a method defined in `AnotherSmallForm` 
                               // class. Just assigning values to elements with `setValue()`

         $form2->saveValues(); // Here is the problem, another method to save the 
// current values to db. (defined in form class). I have to do this in this fragment of code, so i don't know to 
// use which order ( saveValues() and loadValues() methods' order )` 

         $masterform->addSubform($form2, 'form2');
    }
}

所以,第一步:将 $form1 添加到 $masterform。

第二步:$masterform 提交(现在只包含$form1),然后将$form2 添加到$masterform。在添加之前,$form2 的值会加载到表单元素中。

第三步:$masterform 提交,($form1 和 $form2 也是如此)。如果 $form2 中的值发生任何更改,则必须通过此提交在 db 中更新它们。

这是此代码的目标,由于第 3 步无法完成。

【问题讨论】:

  • 我们需要查看更多您的控制器代码。表单提交的值如何进入表单对象?
  • 你在哪里设置数据库的默认值?您可能会覆盖输入值,请尝试更改顺序。如果没有帮助,请在此处发布更多代码。
  • 好的,添加了更多代码,没有表单类中的代码。我将值放入表单元素并保存到数据库的代码工作正常,所以我没有放它们。

标签: php zend-framework zend-form zend-form-element zend-form-sub-form


【解决方案1】:

你有你的问题,在发送帖子值后,你正在用默认的数据库值重写它。

if ($request->isPost()){
.........................
    $form2->loadValues(); // here you're rewriting it!
    $form2->saveValues(); 

简单的改变顺序,先saveValues()再loadValues()。

【讨论】:

  • 是的,知道了。但它不会起作用。因为,$form2 是在提交 $form1 之后创建的。所以它是在这个 `if($request->isPost) 条件块中创建的。在访问那里的第一次迭代中,表单元素没有值(在使用 loadValues() 加载之前),因此它将只有空元素并将空值写入数据库。
  • 您的方法很有用,对于该块的迭代(除了第一次)它会正常工作,所以谢谢。
【解决方案2】:

我终于明白了。即使子表单是在相互关联的条件下创建的,它们也应该在if($request->isPost()) 条件块之外创建。因此,如果您必须通过步骤添加它们(就像我必须这样做,在提交 $form1 之后创建 $form2,并且 $form1 保留在页面中),您应该测试它们的要求是否直接和单独地得到满足。我的这种方法行不通。应该是这样的:

$masterform = new Application_Form_GeneralForm(); 
$form1 = new Application_Form_SmallForm();
$masterform->addSubform($form1, 'form1');                          

$form2 = new Application_Form_AnotherSmallForm(); 

if (/* the requirement you want to check if $form2 should be added or not, 
    and this could easily be checking some value which is submitted with 
    $form1, so you have already checked if $form1 has been submitted */ ) 
    $masterform->addSubform($form2, 'form2'); // we add it if requirement is met

$request = $this->getRequest();
if ($request->isPost()){
    if ($generalform->isValid($request->getPost())) {
         $form2->saveValues(); // be sure it saves the final values added to form
         ////.......
    }
}

而且,我在这里不再使用loadValues(),我在表单类的init() 方法中调用了该方法。所以,正如 konradwww 所说, saveValues() 应该首先发生,然后 loadValues() 接下来发生。这可以通过表单类中的loadValues()来完成,反正属于表单的初始化过程。

【讨论】:

    猜你喜欢
    • 2017-10-28
    • 1970-01-01
    • 2017-08-06
    • 2012-12-29
    • 2011-07-06
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多