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.
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.