Zend表单编辑和Zend_Validate_Db_NoRecordExists [英] Zend Form Edit and Zend_Validate_Db_NoRecordExists

查看:125
本文介绍了Zend表单编辑和Zend_Validate_Db_NoRecordExists的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过建立一些供自己使用的实用程序网站来逐步提高我的Zend技能.我一直在使用Zend Forms和Form验证,到目前为止,我一直很高兴能理解Zend的做事方式.但是,我对如何在编辑表单和映射到必须唯一的数据库列的字段的上下文中使用Zend_Validate_Db_NoRecordExists()感到困惑.

I am slowly building up my Zend skills by building some utility websites for my own use. I have been using Zend Forms and Form validation and so far have been happy that I have been understanding the Zend way of doing things. However I am a bit confused with how to use Zend_Validate_Db_NoRecordExists() in the context of an edit form and a field that maps to database column that has to be unique.

例如,使用此简单表

TABLE Test
(
  ID INT AUTO_INCREMENT,
  Data INT UNIQUE
);

如果我只是向Table Test添加新行,则可以向Zend Form元素的Data字段添加一个验证器,如下所示:

If I was simply adding a new row to the Table Test, I could add a validator to the Zend Form element for the Data field as such:

$data = new Zend_Form_Element_Text('Data');
$data->addValidator( new Zend_Validate_Db_NoRecordExists('Test', 'Data') )

在表单验证中,此验证器将检查表中是否不存在Data元素的内容.因此,可以在不违反数据"字段UNIQUE限定符的情况下继续插入Test.

At form validation this validator will check that the contents of the Data element does not already exist in the table. Thus the insert into Test can go ahead without violating the Data fields UNIQUE qualifier.

但是,在编辑测试"表的现有行时,情况有所不同.在这种情况下,验证器需要检查元素值是否满足以下两个互斥条件之一:

However the situation is different when editing an existing row of the Test table. In that case the validator needs to check that the element value meets one of two mutually exclusive conditions conditions:

  1. 用户已更改元素值,并且当前未更改新值 在表中.

  1. The user has changed the element value, and the new value does not currently exist in the table.

用户已更改了元素值.因此,表中当前存在值确实(可以).

The user has Not changed the element value. Thus the value does currently exist in the table (and this is OK).

Zend验证文档讨论了向其中添加参数NoRecordExists()验证程序,用于从验证过程中排除记录.想法是验证表以查找任何匹配的行,但忽略字段具有此特定值的任何匹配".这样的用例是编辑表时验证元素所需的用例.在1.9中执行此操作的伪代码是这样的(实际上我是从1.9源代码获得的-我认为当前文档可能是错误的):

The Zend Validation Docs talk about adding a parameter to the NoRecordExists() validator for the purpose of excluding records from the validation process. The idea being to "validate the table looking for any matching rows, but ignore any hits where the a field has this specific value". Such a use case is what is needed for the validating the element when editing a table. The pseudo code to do this in 1.9 is like so (actually I got this from the 1.9 source code - I think the current docs may be wrong):

$data = new Zend_Form_Element_Text('Data');
$data->addValidator( new Zend_Validate_Db_NoRecordExists('Test', 'Data',
                     array ('field'=>'Data', 'Value'=> $Value) );

问题在于,要实例化时(同样在实例化窗体时),要排除的值($ Value)绑定到了验证器.但是,当表单正在编辑记录时,当表单最初使用数据填充时,该值需要绑定到$ data字段的内容-IE最初是从Test表行读取的Data值.但是在典型的Zend模式中,在两个单独的步骤中实例化并填充了一个表单,从而避免将exclude值绑定到所需的元素值.

The problem is that the value that is to be excluded ($Value) is bound to the validator at the time it is instantiated (also when the form is instantiated). But when the form is editing a record, that value needs to be bound to the contents of the $data field when the form was initially populated with data - IE the Data value initially read from the Test table row. But in typical Zend patterns a form is instantiated and populated in two separate steps which precludes binding the exclude value to the desired element value.

以下Zend伪代码标记了我希望将$ Value绑定到NoRecordExists()验证器的位置(请注意,这是常见的Zend控制器模式):

The following Zend psuedo code marks where I would like the binding of $Value to the NoRecordExists() validator to occur (and note that this is a common Zend controller pattern):

$form = new Form() 
if (is Post) {
    $formData = GetPostData()
    if ($form->isValid($formData)) {
        Update Table with $formData
        Redirect out of here
    } else {
        $form->populate($formData)
    }
} else {
    $RowData = Get Data from Table
    $form->populate($RowData)     <=== This is where I want ('value' => $Value) bound
}

我可以对Zend_Form进行子类化,并覆盖populate()方法以对初始表单填充进行一次NoRecordExists()验证器的一次性插入,但是对我来说,这似乎是一个巨大的麻烦.所以我想知道其他人的想法,是否已经写下一些解决该问题的模式?

I could sub-class Zend_Form and override the populate() method to do a one-shot insertion of the NoRecordExists() validator on initial form population, but that seems like a huge hack to me. So I wanted to know what other people think and is there some pattern already written down that solves this problem?

编辑2009-02-04

我一直认为,解决此问题的唯一可行方法是编写自定义验证器,而不必担心Zend版本.我的表单将记录ID作为隐藏字段,因此给定表名和列名,我可以编写一些SQL以测试其唯一性,并排除ID这样的行.当然,这让我开始思考如何将表格与模型应该隐藏的dB层联系起来!

I've been thinking that the only decent solution to this problem is to write a custom validator and forget about the Zend version. My form has the record ID as hidden field, so that given the table and column names I could craft some SQL to test for uniqueness and exclude the row with an ID of such an such. Of course this started me thinking about how I would be tying the form to the dB layer that the Model is supposed to hide!

推荐答案

在查看了绝大多数反馈后,我决定要使用自定义验证器

After reviewing the overwhelming response I've decided that I'm going with a custom validator

这篇关于Zend表单编辑和Zend_Validate_Db_NoRecordExists的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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