php – Zend验证Db_NoRecordExists并排除选项

前端之家收集整理的这篇文章主要介绍了php – Zend验证Db_NoRecordExists并排除选项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试为Db_NoRecordExists验证器使用“exclude”选项,因为当我“编辑”该元素时,它总是像往常一样返回“重复”错误.

我的目的是告诉表单保留从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',array(
               'table'     => 'pages','exclude'   => array(
                   'field' => 'id','value' => $this->getValue('id')
               )
           )
       );
  return parent::isValid($data);
}

猜你在找的PHP相关文章