我无法弄清楚为什么我的Zend表单没有更新为新值。我非常密切地关注了Album示例,但无法弄清楚我哪里出错了。目前,Form未通过IsValid IF语句。如果我将保存行放在此检查上方,则无论表单框中的新信息是什么,它似乎都会重新输入旧信息。有什么想法吗?
以下编辑行动:
public function editLicenceAction()
{
$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
$pro_id='22';
$licence_id = (int) $this->params()->fromRoute('licence_id', 0);
if (!$licence_id) {
return $this->redirect()->toRoute('proLicence', array(
'action' => 'index'
));
}
try {
$proLicence = $this->getProLicenceTable()->getProLicence($licence_id);
}
catch (\Exception $ex) {
return $this->redirect()->toRoute('proLicence', array(
'action' => 'index'
));
}
$form = new ProLicenceForm($dbAdapter);
$form->bind($proLicence);
$form->get('submit')->setAttribute('value', 'Edit');
$request = $this->getRequest();
if ($request->isPost()) {
//$form->setInputFilter($proLicence->getInputFilter());
$form->setData($request->getPost());
echo 'I got Request';
if ($form->isValid()) {
echo 'I got Save';
$this->getProLicenceTable()->saveProLicence($proLicence);
return $this->redirect()->toRoute('proLicence');
}
}
return array(
'licence_id' => $licence_id,
'form' => $form,
);
}
见下图:
$title = 'Edit Licence';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<?php
$form = $this->form;
$form->setAttribute('action', $this->url(
'proLicence',
array(
'action' => 'editLicence',
'licence_id' => $this->licence_id,
)
));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('licence_id'));
echo $this->formRow($form->get('licence_name_id'));
echo $this->formRow($form->get('licence_number'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
专业许可表类
<?php
namespace Pro\Model\ProLicence;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Sql;
class ProLicenceTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll()
{
$resultSet = $this->tableGateway->select();
return $resultSet;
}
public function getProLicence($licence_id)
{
$licence_id = (int) $licence_id;
$rowset = $this->tableGateway->select(array('licence_id' => $licence_id));
$row = $rowset->current();
if (!$row) {
throw new \Exception("Could not find row $licence_id");
}
return $row;
}
public function saveProLicence(ProLicence $proLicence)
{
$data = array(
'pro_id' =>$proLicence->pro_id,
//'licence_id' => $proLicence->licence_id,
'licence_name_id' => $proLicence->licence_name_id,
'licence_number' => $proLicence->licence_number,
'licence_approved' => $proLicence->licence_approved,
);
$licence_id = (int) $proLicence->licence_id;
if ($licence_id == 0) {
$this->tableGateway->insert($data);
} else {
if ($this->getProLicence($licence_id)) {
$this->tableGateway->update($data, array('licence_id' => $licence_id));
} else {
throw new \Exception('Licence id does not exist');
}
}
}
public function deleteProLicence($licence_id)
{
$this->tableGateway->delete(array('licence_id' => (int) $licence_id));
}
}
专业版级别
<?php
namespace Pro\Model\ProLicence;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class ProLicence implements InputFilterAwareInterface
{
public $pro_id;
public $licence_id;
public $licence_name_id;
public $licence_number;
public $licence_approved;
protected $inputFilter;
public function exchangeArray($data)
{
$this->pro_id = (!empty($data['pro_id'])) ? $data['pro_id'] : null;
$this->licence_id = (!empty($data['licence_id'])) ? $data['licence_id'] : null;
$this->licence_name_id = (!empty($data['licence_name_id'])) ? $data['licence_name_id'] : null;
$this->licence_number = (!empty($data['licence_number'])) ? $data['licence_number'] : null;
$this->licence_approved = (!empty($data['licence_approved'])) ? $data['licence_approved'] : null;
}
// Add content to these methods:
public function getArrayCopy()
{
return get_object_vars($this);
}
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$inputFilter->add(array(
'name' => 'pro_id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
));
$inputFilter->add(array(
'name' => 'licence_id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
));
$inputFilter->add(array(
'name' => 'licence_name_id',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'licence_number',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'licence_approved',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
FORM类:
<?php
namespace Pro\Form;
use Zend\Form\Form;
use Zend\Db\Adapter\AdapterInterface;
class ProLicenceForm extends Form
{
public function __construct(AdapterInterface $dbAdapter)
{
$this->adapter =$dbAdapter;
// we want to ignore the name passed
parent::__construct();
$this->add(array(
'name' => 'pro_id',
'type' => 'Hidden',
));
$this->add(array(
'name' => 'licence_id',
'type' => 'Hidden',
'options' => array(
),
'attributes' => array(
//'required' => 'required'
)
));
$this->add(array(
'name' => 'licence_approved',
'type' => 'Hidden',
'options' => array(
),
'attributes' => array(
// 'required' => 'required'
)
));
$this->add(array(
'name' => 'licence_name_id',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Licence',
'value_options' => $this->getLicenceOptions(),
'empty_option' => '--- please choose ---'
),
'attributes' => array(
'placeholder' => 'Choose all that apply'
)
));
$this->add(array(
'name' => 'licence_number',
'type' => 'Text',
'options' => array(
'label' => 'Licence Number'
)
));
$this->add(array(
'name' => 'submit',
'type' => 'Submit',
'options' => array(
'label'=> 'Primary Button',
),
'attributes' => array(
'value' => 'Go',
'id' => 'submitbutton',
),
));
}
/*
* SQL statements used to bring in optiosn
*/
public function getLicenceOptions()
{
$dbAdapter = $this->adapter;
$sql = 'SELECT licence_name_id,licence_name FROM licence_name_table ORDER BY licence_name';
$statement = $dbAdapter->query($sql);
$result = $statement->execute();
$selectData = array();
foreach ($result as $res) {
$selectData[$res['licence_name_id']] = $res['licence_name'];
}
return $selectData;
}
}
非常感谢,M
更新:以下作品(从专辑示例中复制)。这就是我用来解决问题的麻烦。
public function editAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('album', array(
'action' => 'add'
));
}
// Get the Album with the specified id. An exception is thrown
// if it cannot be found, in which case go to the index page.
try {
$album = $this->getAlbumTable()->getAlbum($id);
}
catch (\Exception $ex) {
return $this->redirect()->toRoute('album', array(
'action' => 'test'
));
}
$form = new AlbumForm();
$form->bind($album);
$form->get('submit')->setAttribute('value', 'Edit');
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($album->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$this->getAlbumTable()->saveAlbum($album);
Debug::dump($form);
//var_dump($album);
// Redirect to list of albums
//return $this->redirect()->toRoute('album');
}
}
return array(
'id' => $id,
'form' => $form,
);
}
更新2:比较调试似乎下面的部分没有在许可证示例上更新,但是在工作专辑示例中:
["object":protected] => object(Pro\Model\ProLicence\ProLicence)#398 (6) {
["pro_id"] => string(2) "22"
["licence_id"] => string(1) "1"
["licence_name_id"] => string(1) "5"
["licence_number"] => string(9) "345345345"
["licence_approved"] => string(1) "T"
答案 0 :(得分:0)
您是否为表单设置了方法?如果我没记错,默认方法是GET,但是您使用POST检索提交的值。尝试将表单方法明确设置为POST。
您还可以尝试使用Zend\Debug\Debug::dump($form)
查看对象在不同位置包含的内容 - 但实际上您似乎并未从提交的表单中获取新值。
用于设置表单数据的方法似乎不正确 - 它应该类似于
$form->setData($this->params()->fromPost());
因为你想要的只是关联数组中的数据元素。
再次查看你的代码,错误是相当明显的 - 只是另一组眼睛!你打电话:
$this->getAlbumTable()->saveAlbum($album);
但您尚未在$ album对象中设置数据。
答案 1 :(得分:0)
我忘了将隐藏的表单添加到我的编辑字段中。一旦添加它们,我就可以编辑没有问题。
echo $this->formHidden($form->get('membership_id'));
echo $this->formHidden($form->get('pro_id'));