0

我在添加隐藏的 zend 表单元素时遇到了麻烦。

当我调用 addElements 时,表单失败并将以下错误打印到页面。但只有当我尝试添加 $formContactID 和 $formCustomerID 时。

致命错误:在第 3291 行的 /home/coder123/public_html/wms2/library/Zend/Form.php 中的非对象上调用成员函数 getOrder()

我的代码如下。

  private function buildForm()
 {
  $Description = "";
  $FirstName = "";
  $LastName = "";
  $ContactNumber = "";
  $Fax = "";
  $Position = "";
  $Default = "";
  $custAddressID = "";
  $CustomerID = "";
  $Email = "";
  $ContactID = "";
  if($this->contactDetails != null)
  {
   $Description = $this->contactDetails['Description'];
   $CustomerID = $this->contactDetails['CustomerID'];
   $FirstName = $this->contactDetails['FirstName'];
   $LastName = $this->contactDetails['LastName'];
   $ContactNumber = $this->contactDetails['ContactNumber'];
   $Position = $this->contactDetails['Position'];
   $Fax = $this->contactDetails['Fax'];
   $Email = $this->contactDetails['Email'];
   $Default = $this->contactDetails['Default'];
   $custAddressID = $this->contactDetails['custAddressID'];
   $ContactID = $this->contactDetails['custContactID'];
  }


  $formfirstname = new Zend_Form_Element_Text('FirstName');
  $formfirstname->setValue($FirstName)->setLabel('First Name:')->setRequired();

  $formlastname = new Zend_Form_Element_Text('LastName');
  $formlastname->setLabel('Last Name:')->setValue($LastName)->setRequired();

  $formPhone = new Zend_Form_Element_Text('ContactNumber');
  $formPhone->setLabel('Phone Number:')->setValue($ContactNumber)->setRequired();

  $formFax = new Zend_Form_Element_Text('FaxNumber');
  $formFax->setLabel('Fax Number:')->setValue($Fax);

  $FormPosition = new Zend_Form_Element_Text('Position');
  $FormPosition->setLabel('Contacts Position:')->setValue($Position);

  $FormDescription = new Zend_Form_Element_Text('Description');
  $FormDescription->setLabel('Short Description:')->setValue($Description)->setRequired();

  $formEmail = new Zend_Form_Element_Text('Email');
  $formEmail->setLabel('Email Address:')->setValue($Email);

  $FormDefault = new Zend_Form_Element_Checkbox('Default');
  $FormDefault->setValue('Default')->setLabel('Set as defualt contact for this business:');

  if($Default == 'Default')
  {
   $FormDefault->setChecked(true);
  }

  $formCustomerID = new Zend_Form_Element_Hidden('customerID');
  $formCustomerID->setValue($customerID);
  if($this->contactID != null)
  {
   $formContactID = new Zend_Form_Element_Hidden('ContactID');
   $formContactID->setValue($this->contactID);
  }

// FORM SELECT 
  $formSelectAddress = new Zend_Form_Element_Select('custAddress');
  $pos = 0;
  while($pos < count($this->customerAddressArray))
  {
   $formSelectAddress->addMultiOption($this->customerAddressArray[$pos]['custAddressID'], $this->customerAddressArray[$pos]['Description']);
   $pos++;
  }
  $formSelectAddress->setValue(array($this->contactDetails['custAddressID']));
  $formSelectAddress->setRequired()->setLabel('Default Address For this Contact:');
// END FORM SELECT

  $this->setMethod('post');
  $this->setName('FormCustomerEdit');

  $formSubmit = new Zend_Form_Element_Submit('ContactSubmit');
  $formSubmit->setLabel('Save Contact');

  $this->setName('CustomerContactForm');
  $this->setMethod('post');


  $this->addElements(array($FormDescription, $formfirstname, $formlastname, 
         $FormPosition, $formPhone, $formFax, $FormDefault, 
         $formEmail, $formSelectAddress, $formContactID, $formCustomerID, $formSubmit));

  $this->addElements(array($formSubmit));

 }
4

2 回答 2

2

也许你已经修好了,但以防万一。

我遇到了同样的问题,问题是表单中某些属性的名称。在您的情况下,您有:

  if($this->contactID != null){
   $formContactID = new Zend_Form_Element_Hidden('ContactID');
   $formContactID->setValue($this->contactID);
  }

在您将 $formContactID 添加到表单的那一刻,已经为表单对象创建了一个新的内部属性,即“ContactID”。所以现在我们有了 $this->ContactID 和 $this->contactID。

根据 PHP 标准,这应该不是问题,因为关联数组键和对象属性名称区分大小写,但我只是想使用您的代码来说明 Zend Form 的行为。

修改表单中的其余代码,以确保您没有覆盖任何 Zend 元素。抱歉猜测,但由于您没有发布表单文件的所有代码,因此调试起来有点困难。

谢谢,我希望它有所帮助。

于 2013-02-19T14:59:21.577 回答
0

我认为问题在于$this->addElements何时$formContactIDif($this->contactID != null)规则而丢失。

您可以更新代码以解决问题:

.....
$this->addElements(array($FormDescription, $formfirstname, $formlastname, 
      $FormPosition, $formPhone, $formFax, $FormDefault, 
      $formEmail, $formSelectAddress, $formCustomerID, $formSubmit));

if(isset($formContactID)) {
  $this->addElements(array($formContactID));
}
......
于 2010-11-04T14:14:13.440 回答