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

g089h515r806’s picture

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

honigferd’s picture

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

g089h515r806’s picture

@honigferd,
Test the code provided by gionnibgud:

if($this->rule->entity_type == 'field_collection_item' && arg(0) == 'field-collection' && arg(3) =='edit' && empty($id)) { 
 $id = arg(2); 
}

It should fix your issue.

honigferd’s picture

Hm. Where would I put that code? Sorry for a maybe dumb question. :-S

honigferd’s picture

Also this code seems to be specific to field collections, which I don't use. :-S

g089h515r806’s picture

The 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

honigferd’s picture

Ok, 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:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'profile2');
$query->entityCondition('bundle', 'your_profile_type');
$query->fieldCondition('field_your_field', 'value', $this->value, '=');

if (arg(1)) {
  $query->propertyCondition('uid', arg(1), '<>');
}

$flag = (bool)$query->range(0,1)->count()->execute();

if ($flag) {
  form_set_error('field_your_field', $this->get_error_message());
}

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.

cursor’s picture

Thanks for that. It worked well. Maybe we should try to Profile2 / Field Validation to support this directly.

honigferd’s picture

That 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. :(

cursor’s picture

At 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 :)

g089h515r806’s picture

It is ok to open it at here.

For the code, we could use

$this->set_error();

instead of

form_set_error('field_your_field', $this->get_error_message());

it is a little more simple.

zambrey’s picture

I created a new issue with a patch related to Profile2 integration.
You can find it here: #1855106: Support unique validation on Profile2 fields

g089h515r806’s picture

Status: Active » Closed (fixed)