Hi
I have a field collection with 2 fields, a date and a text field.
Unique validation (bundle scope) is configured for the date field.
The validation works correctly during the field collection item creation but if I try to edit the item I get a validation error message even if the date field doesn't change value.
I think the problem is that
list($id, $vid, $bundle) = entity_extract_ids($this->rule->entity_type, $this->entity);
return an empty $id for the field collection so
$query->entityCondition('entity_id', $id, '!=');
this condition doesn't make it in to the query.
This workaround works:
if($this->rule->entity_type == 'field_collection_item' && arg(0) == 'field-collection' && arg(3) =='edit' && empty($id)) {
$id = arg(2);
}
It's the same approach as per the user entity in the same plugin.
I'm planning on duplicating the plugin and add a custom one from my custom module implementing this approach.
Not sending a patch unless this is accepted as a correct approach to the problem.
Cheers and thanks for this module.
Comments
Comment #1
g089h515r806 commented(1)We could use your code directly and solve this issue, this is the faster way to fixed this issue.
(2)Actually speaking, this is a bug of field collection module. field collection should make sure that entity_extract_ids works correctly. The best way is to fix it in field collection module. Only a few people find this issue.
Comment #2
honigferd commentedHm. I have that the same problem (I think?) with a Unique field validation without field collection:
I have a single, independet field, set the validation to scope global and whenever I try to edit the node/profile without changing the unique field I get the error message I specified.
Comment #3
g089h515r806 commented@honigferd,
Test the code provided by gionnibgud:
It should fix your issue.
Comment #4
honigferd commentedHm. Where would I put that code? Sorry for a maybe dumb question. :-S
Comment #5
honigferd commentedAlso this code seems to be specific to field collections, which I don't use. :-S
Comment #6
g089h515r806 commentedThe code has been commited.
If the problem still exists, that is because entity_extract_ids return an empty $id for the field collection, this is a bug of field collection modulem
Comment #7
honigferd commentedOk, this is specific to Profile2 fields seeing "themselves" as duplicates when editing the profile, not Field Collection, but as I posted my initial issue here this is a followup for completion.
The biggest problem seems to be that the Profile2 entity ( $this->entity ) doesn't seem to come with any form of pid or uid, just the fields and the profile type.
After a lot of fiddling around I ended up using this more or less dirty workaround, not using the Unique Validation but a PHP validation with the following Code:
This code is, with arg(1) retrieving the user ID of the Profile2 profile from the URI. As with editing profiles and their permissions it's not pretty but it shouldn't have any major concern and it seems to work for now as a Unique Validator.
This solution is specific to Profile2, as said, but maybe it can help someone else save some time on this issue.
Comment #8
cursor commentedThanks for that. It worked well. Maybe we should try to Profile2 / Field Validation to support this directly.
Comment #9
honigferd commentedThat would be great, although I think trying to get the User ID or the Profile ID into the entity would be the cleaner approach for that, instead of using arg()
Unfortunately I couldn't quite figure out how to do that. :(
Comment #10
cursor commentedAt the moment it works rather well. I am happy but we need to get an issue open. Not sure, who needs to support this though - Profile2 or Field validation :)
Comment #11
g089h515r806 commentedIt is ok to open it at here.
For the code, we could use
instead of
it is a little more simple.
Comment #12
zambrey commentedI created a new issue with a patch related to Profile2 integration.
You can find it here: #1855106: Support unique validation on Profile2 fields
Comment #13
g089h515r806 commented