当我添加元素时,Zend_Form_Element失败

时间:2010-11-04 03:22:21

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

我在添加隐藏的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));

 }

2 个答案:

答案 0 :(得分:2)

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

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

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

在您向表单添加$ formContactID时,已经为表单对象创建了一个新的内部属性,这就是'ContactID'。所以现在我们有$ this-&gt; ContactID和$ this-&gt; contactID。

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

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

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

答案 1 :(得分:0)

由于$this->addElements规则导致$formContactID丢失,我认为问题出在if($this->contactID != null)上。

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

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

if(isset($formContactID)) {
  $this->addElements(array($formContactID));
}
......