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?
Make this changes to the form:
And to the edit action:
I had a similar problem. My solution was to move the validator from the
init
to theisValid
function.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:
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.
There is no need to build the validator in the
isValid
function, just update theexclude
clause:For me only this solution works perfectly:
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:
At that point it will pass the isValid logic no problem.
Enjoy!