我在Zend Framework 1中有一个表单。当我点击编辑按钮时,我想在表单中显示数据库中的值。但我不知道怎么做。
这是我的表单代码:
// Add an email element
$this->addElement('text', 'orgname', array(
'required' => true,
'filters' => array('StringTrim'),
'style' => array('width:220px'),
'decorators'=>Array(
'ViewHelper','Errors'
)
));
这是我的控制者:
public function editclientcompanyAction()
$form = new Application_Form_Companyform();
$form->addform();
$this->view->form = $form;
$request = $this->getRequest();
$editid=$request->getParam('id');
$edit_show = new Application_Model_Clientcompany;
$showdetails = $edit_show->editclient($editid);
$this->view->assign('datas', $showdetails);
如何在Zend表单中显示数据库版本?
答案 0 :(得分:3)
有两种情况。
1)填充具有与数据库表字段相同的字段的表单:如果您的表单具有与数据库字段相同的字段,则可以轻松填充它们。 首先,您需要从数据库中获取数据,然后调用Zend_Form填充函数将数据作为关联数组传递,其中键与表单字段名称相同,值将是表单字段的值,如下所示。形式。
这将在你的控制器中
$data = array("orgname" => "Value for the field");
$form = new Application_Form_Companyform();
$form->populate($data);
现在send会自动填充表单字段orgname。您无需修改表单或在addElement中设置值字段。
* 2)手动设置字段值:* 第二种情况是手动设置值。首先,您需要修改表单并为其添加构造函数。同样在您的表单类中,您将需要创建一个属性(如果您有多个字段,那么您可以为每个字段创建一个数组属性或多个属性。这将取决于您。)。然后在addElement中设置值键。您的表单应如下所示
class Application_Form_Companyform extends Zend_Form
{
private $orgname;
public function __contruct($orgname)
{
$this->orgname = $orgname;
//It is required to call the parent contructor, else the form will not work
parent::__contruct();
}
public function init()
{
$this->addElement('text', 'orgname',
array(
'required' => true,
'filters' => array('StringTrim'),
'style' => array('width:220px'),
'decorators'=>Array('ViewHelper','Errors'),
'value'=>$this->orgname
)
));
} //end of init
} //end of form
现在你的控制器,你需要实例化表单对象传递orgname字段的值,如下所示
$form = new Application_Form_Companyform("This is the value for orgname");
就是这样。
我使用了这样的方法,它就像一个魅力。根据您的要求,您可能需要调整上面的示例代码,因为我没有检查它,但它会运行正常,我希望:P
谢谢
答案 1 :(得分:0)
在ZF1或ZF2中都可以。只需这样做。
// Add an email element
$this->addElement('text', 'orgname',
array(
'required' => true,
'filters' => array('StringTrim'),
'style' => array('width:220px'),
'decorators' => Array('ViewHelper','Errors'),
'value' => $showdetails->orgname
)
));
您可能希望首先测试null / empty值,但为方便起见,您可以使用三元运算符:
// Add an email element
$this->addElement('text', 'orgname',
array(
'required' => true,
'filters' => array('StringTrim'),
'style' => array('width:220px'),
'decorators' => Array('ViewHelper','Errors'),
'value' => empty($showdetails->orgname)? null : $showdetails->orgname
)
));
答案 2 :(得分:0)
请查看我在/var/www/html/zend1app/application/controllers/CountryController.php的编辑功能:
public function editAction() {
$data = $this->getRequest()->getParams();
$id = (int)$data['id'];
$options = array();
$country = $this->getCountryModel()->fetchRow("id=$id");
if(!$country)
{
throw new Exception("Invalid Request Id!");
}
$form = new Application_Form_Country();
$form->addIdElement();
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost())){
$data = new Application_Model_Country();
if($data->save($form->getValues()))
{
$message = array("sucess" => "Country has been updated!");
}
else {
$message = array("danger" => "Country could not be updated!");
}
$this->_helper->FlashMessenger->addMessage($message);
return $this->_helper->redirector('index');
}
}
$options = array (
'id' => $country->id,
'name' => $country->name,
'code' => $country->code
);
$form->populate( $options ); // data binding in the edit form
$this->view->form = $form;
}
并在/var/www/html/zend1app/application/forms/Country.php上形成课程:
class Application_Form_Country extends Zend_Form
{
public function init()
{
// Set the method for the display form to POST
$this->setMethod('post');
// Add an email element
$this->addElement('text', 'name', array(
'label' => 'Enter Country Name:',
'required' => true,
'filters' => array('StringTrim'),
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 20))
)
));
// Add the comment element
$this->addElement('text', 'code', array(
'label' => 'Enter Country Code:',
'required' => true,
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 20))
)
));
// Add the submit button
$this->addElement('submit', 'submit', array(
'ignore' => true,
'label' => 'Save',
));
// And finally add some CSRF protection
$this->addElement('hash', 'csrf', array(
'ignore' => true,
));
}
public function addIdElement()
{
$this->addElement('hidden', 'id');
}
}
HTH