gpt4 book ai didi

php - Zend 验证 Db_NoRecordExists 和排除选项

转载 作者:可可西里 更新时间:2023-11-01 12:43:20 24 4
gpt4 key购买 nike

我正在尝试对 Db_NoRecordExists 验证器使用“排除”选项,因为当我“编辑”元素时,它总是像往常一样返回“重复”错误。

我的目标是告诉表单保留从 Controller 传递给表单本身的值...

这是 Controller :

public function editAction()
{
$id = $this->getRequest()->getParam('id');
$pagesMapper = new Application_Model_PagesMapper();
$form = new Application_Form_PageEdit();
$form->populate($pagesMapper->fetchId($id, true));
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost())) {
//... cut ...
}
}
$this->view->form = $form;
}

这是表格:

class Application_Form_PageEdit extends Zend_Form
{
public function init()
{
$commonFilters = array('StringTrim');
$commonValidators = array('NotEmpty');
$this->setMethod('post')->setAction('/admin-page/edit');

$id = new Zend_Form_Element_Hidden('id');
$pid = new Zend_Form_Element_Hidden('pid');

$keyname = new Zend_Form_Element_Text('keyname');
$keyname->setLabel('Keyname')
->setRequired(true)
->addFilters($commonFilters)
->addFilter('StringToLower')
->addFilter('Word_SeparatorToDash')
->addValidator('Db_NoRecordExists', false, array(
'table' => 'pages',
'field' => 'keyname',
'exclude' => array(
'field' => 'id',
'value' => $this->getValue('id)
)
)
);

//...剪切...

一些建议?

最佳答案

我遇到了类似的问题。我的解决方案是将验证器从 init 移至 isValid 函数。

public function isValid($data)
{
$this->getElement('keyname')
->addValidator(
'Db_NoRecordExists',
false,
array(
'table' => 'pages',
'field' => 'keyname',
'exclude' => array(
'field' => 'id',
'value' => $this->getValue('id')
)
)
);
return parent::isValid($data);
}

关于php - Zend 验证 Db_NoRecordExists 和排除选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5378834/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com