从验证器中排除字段的最佳方法 [英] Best approach on excluding fields from a validator

查看:26
本文介绍了从验证器中排除字段的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过扩展 Zend_Form 创建我的表单.我为 addAction()editAction() 使用一个表单.当我想在编辑过程中删除元素时,我可以通过 $form->removeElement('x') 轻松完成.

I create my forms via extending Zend_Form. And I use one Form for addAction() and editAction(). When I want to remove Elements within the editing process I can do so easily via $form->removeElement('x').

但是从验证器中删除字段的最佳方法是什么?

But what would be the best approach on removing a field from the validator?

1) 删除和添加新设置的验证器

1) Removing and Adding the newly set validator

//Controllers editAction()
$form->removeValidator('Db_NoRecordExists');
$form->addValidator('Db_NoRecordExists', true, array(
  'table'=>'table', 
  'field'=>'field',
  'exclude'=>array(
    'field'=>'id',
    'value'=>$this->_getParam('id')
  )
));

2) 将编辑 ID 注入表单

2) Injecting editing ID into the Form

//Forms Contstructor
public function __construct($idToEdit=0, $options=null)
{
   $this->setIdToEdit($idToEdit);
   parent::__construct($options);
}

//within init()
$formField->addValidator('Db_NoRecordExists', true, array(
  'table'=>'table', 
  'field'=>'field',
  'exclude'=>array(
    'field'=>'id',
    'value'=>$this->getIdToEdit()
  )
));

//Controller calling the form like this:
$form = new Custom_Form($this->_getParam('id'), $options);

3) 还有什么?也许我还缺少其他一些东西,对我来说,尽管不知何故,这两个想法对我来说都不太好.

3) Something else? Maybe there is even something else I am missing, to me though somehow both ideas don't look too well to me.

推荐答案

为了更清晰地使用 SO,这里将答案作为帖子

For a cleaner use of SO here the answer as a post

//SOLUTION 好的,所以在浏览 Zends 源代码时(应该在询问之前这样做......)我找到了最好的解决方案(我猜).Abstract DB Validation 类得到了一个函数 setExclude() 这样我们就可以在一个很好的流程中使用它:

//SOLUTION Okay, so while browsing to Zends Sourcecode (should have done that before asking...) i found the best solution (i guess). The Abstract DB Validation classes got a function setExclude() so we can then use it in a nice flow:

//Inside Controller before valling $form->isValid()
$form->getElement('x')->getValidator('Db_NoRecordExists')->setExclude(array(
  'field'=>'some_id',
  'value'=>$idToEdit
))

这篇关于从验证器中排除字段的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆