sfValidatorDoctrineUnique 在更新/编辑上下文中
我在两个不同的上下文中使用表单类:既创建新记录又编辑该记录。我设置了一个后验证器,如下所示,以检查 URL 字段是否唯一。
$this->validatorSchema->setPostValidator(new sfValidatorAnd(array(
new sfValidatorDoctrineUnique(array('model' => 'Page', 'column' => array('url')), array('invalid' => 'This URL already exists.'))
)));
当我创建新记录时,验证器工作得很好。但是,在编辑现有记录时,它会抛出错误,因为它将自身检测为重复项。换句话说,如果我编辑记录但不对 URL 进行任何更改,则会引发重复错误。
这肯定是一个常见问题,所以我想知道 Symfony 处理这个问题的方式是什么?基本上我希望它在保存时忽略自身(不存在重复项),但仍然运行后验证器以确保不存在真正的重复项。
I'm using a form class in two separate contexts: both to create a new record and also to edit that record. I've set up a post-validator as follows to check that the URL field is unique.
$this->validatorSchema->setPostValidator(new sfValidatorAnd(array(
new sfValidatorDoctrineUnique(array('model' => 'Page', 'column' => array('url')), array('invalid' => 'This URL already exists.'))
)));
The validator works great when I'm creating a new record. However, when editing an existing record, it throws an error because it detects itself as a duplicate. In other words, if I edit the record but make no changes to the URL, it throws a duplicate error.
This must be a common issue so I'm wondering what the Symfony way of dealing with this would be? Basically I'd like it to ignore itself when saving (no duplicate exists) but still run the post-validator to ensure no true duplicates exist.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新情况确实是由 sfValidatorDoctrineUnique 处理的。
在您的情况下,如果具有给定 URL 的对象已存在,验证器将检查您是否正在执行更新操作。使用 sfValidatorDoctrineUnique::isUpdate() 方法进行检查。
您的主键需要位于提交的值中。
默认情况下,主键是内省的。您可以为其提供传递给验证器的 *primary_key* 选项。
The update situation is indeed handled by sfValidatorDoctrineUnique.
In your case if object with given URL already exists validator will check if you're performing an update operation. Check is made with sfValidatorDoctrineUnique::isUpdate() method.
Your primary key(s) need to be in the submitted values.
By default primary key is introspected. You can provide it with *primary_key* option passed to the validator.
正如其他答案中所述,确保以下几点非常重要:
sfValidatorDoctrineUnique
验证器必须了解随表单提交的所有值。为了实现此目的,您必须执行以下步骤:
添加一个隐藏输入,其中(通常)包含对象的 PK 值:
将
sfValidatorDoctrineUnique
移至后验证阶段:您需要使用
$this->mergePostValidator()
将验证器添加到后验证阶段,以便提供所有提交的值给验证器。请注意,您仍然需要为唯一列小部件提供验证器,否则您在提交表单时将收到“意外的额外表单字段”错误。
确保将要更新的对象传递给表单的构造函数:
As noted in other answers, it is important to make sure of the following:
sfValidatorDoctrineUnique
validator must be aware of all values submitted with the form.In order to accomplish this, you must perform the following steps:
Add a hidden input that contains (usually) the PK value for your object:
Move the
sfValidatorDoctrineUnique
to the post-validation phase:You will need to use
$this->mergePostValidator()
to add the validator to the post-validation phase so that all submitted values are provided to the validator.Note that you still need to supply a validator for the unique column widget, or else you will get an "Unexpected extra form field" error when you submit the form.
Ensure that you are passing the object being updated to the constructor of your form: