从验证器中排除字段的最佳方法
我通过扩展 Zend_Form 创建表单。我对 addAction()
和 editAction()
使用一个表单。当我想在编辑过程中删除元素时,我可以通过 $form->removeElement('x')
轻松完成。
但是从验证器中删除字段的最佳方法是什么?
1) 删除和添加新设置的验证器
//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 注入表单
//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) 还有其他吗? 也许我什至还缺少其他东西,尽管对我来说,这两种想法对我来说都不太好。
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) 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) 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) Something else?
Maybe there is even something else I am missing, to me though somehow both ideas don't look too well to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了更清晰地使用 SO,这里将答案作为帖子
//解决方案 好的,所以在浏览 Zends 源代码时(应该在询问之前完成此操作...)我找到了最好的解决方案(我猜)。抽象数据库验证类有一个
函数 setExclude() 这样我们就可以在一个很好的流程中使用它:
For a cleaner use of SO here the answer as a post
//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: