Home » Php » php – Zend Validation Db_NoRecordExists and exclude option

php – Zend Validation Db_NoRecordExists and exclude option

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m trying to use the “exclude” option for a Db_NoRecordExists validator, cause when I’m “editing” the element it always return me back a “duplicated” error, as usual.

What I aim to is to tell to the form to keep back the value passed to the form itself from the Controller…

This is the 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;
}

This is the 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)
        )
        )
    );

//… cut …

Some advices?

How to&Answers:

I had a similar problem. My solution was to move the validator from the init to the isValid function.

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

Answer:

For me only this solution works perfectly:

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

Answer:

What I do, before the validation logic in the controller, I add the following code to check the field against the current user data. If its a match, I just remove the validator from the element like so:

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

At that point it will pass the isValid logic no problem.

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

Enjoy!

Answer:

I didn’t like the solution of overriding the isValid() function. This just feels like we’re not understanding how the Db_NoRecordExists validator works. You simply need to provide the validator with the actual ID of the record to exclude prior to calling isValid(). The validator even provides an accessor to set this value right within itself! The Zend instructions really don’t help out as well as they could either, so it’s not surprising people are struggling with this.

Here’s a more elegant and work-flow oriented way to set the exclude without hacking, extending or overriding the form:

// $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
    // ...
}

The exclude is actually an array of our field and value. We just tell the validator what to exclude just prior to calling isValid(). Done. Simple.

Answer:

Make this changes to the form:

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

And to the edit action:

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

Answer:

There is no need to build the validator in the isValid function, just update the exclude clause:

public function isValid($data)
{
    $this->getElement('name')->getValidator('Db_NoRecordExists')->setExclude('id != ' . $data['id']);
    return parent::isValid($data);
}