7

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

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

这是控制器:

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)
        )
        )
    );

//... 切 ...

一些建议?

4

6 回答 6

17

我有一个类似的问题。我的解决方案是将验证器从函数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);
}
于 2011-03-21T14:46:04.710 回答
2

对我来说,只有这个解决方案才能完美运行:

public function isValid($data)
{
    $clause    = $this->_dbAdapter->quoteInto('id = ?', $this->getValue('id'));
    $this->getElement('keyname')
        ->addValidator('Db_NoRecordExists', false, array(
            'adapter'   =>  $this->_dbAdapter,
            'table'     => 'employee_name',
            'field'     => 'name',
            'exclude'   => $clause
        )
    );
    return parent::isValid($data);
}
于 2011-10-28T14:59:46.820 回答
1

我所做的是,在控制器中的验证逻辑之前,我添加以下代码来检查当前用户数据的字段。如果匹配,我只需从元素中删除验证器,如下所示:

    if(($this->getRequest()->isPost()) && $request->getParam('email')==$this->user->getEmail()) {
        $form_preferences->getElement('email')->removeValidator('Db_NoRecordExists');
    }

那时它将通过 isValid 逻辑没有问题。

if(($this->getRequest()->isPost()) && $form_preferences->isValid($_POST)) {

享受!

于 2014-01-31T16:12:31.010 回答
1

我不喜欢覆盖isValid()函数的解决方案。这感觉就像我们不了解Db_NoRecordExists验证器的工作原理。您只需在调用isValid()之前向验证器提供要排除的记录的实际 ID 。验证器甚至提供了一个访问器来设置这个值!Zend 指令确实没有提供尽可能好的帮助,所以人们为此苦苦挣扎也就不足为奇了。

这是一种更优雅且面向工作流的方式来设置排除,而无需破解、扩展或覆盖表单:

// $post is assumed to have our posted form variables // 
// $form is assumed to be our form that is already setup with validators. //

$form->getElement('username')->
        getValidator('Db_NoRecordExists')->
        setExclude([
            'field' => 'user_id',
            'value' => $post['user_id'],
        ]);

if ( $form->isValid( $post ) ) {
    // do valid stuff
    // ...
}
else {
    // do invalid stuff
    // ...
}

exclude 实际上是我们的字段和值的数组。我们只是在调用isValid()之前告诉验证器要排除什么。完毕。简单的。

于 2016-09-25T08:03:46.490 回答
0

对表单进行以下更改:

class Application_Form_PageEdit extends Zend_Form
{
    public function getExcludeFromQuery($elementName)
    {
        $element = $this->getElement($elementName);
        if ($element)
        {
            return $element->getValidator('Db_NoRecordExists')->getExclude();
        } else
        {
            return NULL;
        }
    }

    public function setExcludeFromQuery($elementName, $excludeFromQuery)
    {
        $element = $this->getElement($elementName);
        if ($element)
        {
            $element->getValidator('Db_NoRecordExists')->setExclude($excludeFromQuery);
        }
        return $this;
    }


    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)
            )
            )
        );

对于编辑操作:

public function editAction()
{
$duplicateElementName = 'whatever';
$duplicateElementValue = 'value from db';

$id = $this->getRequest()->getParam('id');
$pagesMapper = new Application_Model_PagesMapper();
$form = new Application_Form_PageEdit();
$form->populate($pagesMapper->fetchId($id, true));

$form->setExcludeFromQuery($duplicateElementName, $duplicateElementValue);

if ($this->getRequest()->isPost()) {
    if ($form->isValid($this->getRequest()->getPost())) {
        //... cut ...
    }
}
$this->view->form = $form;
}
于 2012-08-10T16:34:47.787 回答
0

无需在isValid函数中构建验证器,只需更新exclude子句:

public function isValid($data)
{
    $this->getElement('name')->getValidator('Db_NoRecordExists')->setExclude('id != ' . $data['id']);
    return parent::isValid($data);
}
于 2013-01-29T18:25:50.260 回答