UniqueEntity in edit/update action #6651

Closed
md81 opened this Issue Jan 10, 2013 · 8 comments

Projects

None yet

6 participants

@md81
md81 commented Jan 10, 2013

In entity I use:
@UniqueEntity(fields={"email"}, groups={"new", "edit"})

but i have a problem when I try validate in edit mode.
When I submit form with not changed email field, then I get error "This value is already used."

@stof
Member
stof commented Jan 10, 2013

@md81 Are you updating the managed entity or a new instance in your edit action ?

@md81
md81 commented Jan 10, 2013

In update action, I use managed entity, like this:
$editForm = $this->createForm($formType, $entity);

@lskowronski

Hmm, I often have the same problem, but at this moment I can't find any code to paste.

@md81
md81 commented Jan 14, 2013

Does anyone know how to fix it?

@md81
md81 commented Jan 15, 2013

I've found clue.

Problem exists only in case with cascade validation.

when I use in EditType:
$builder
->add('email', 'email')

validation is ok,
but problem is when I use:
$builder
->add('userEdit', new UserEditType(), array(
'validation_groups'=>array('edit', 'company'),
))

where UserEditType contain:
$builder
->add('email', 'email')

@francoispluchino

The problem also exists when you use a different EntityManager in constraint :
@UniqueEntity(fields="name", em="my_custom_em").

@kor3k
kor3k commented Feb 11, 2013

i have this issue with FOSUserBundle's ProfileFormType. i am using non-default entity manager and it gives me same results while editing/updating.

@kor3k
kor3k commented Feb 11, 2013

the problem seems to be in
Symfony\Bridge\Doctrine\Validator\ConstraintsUniqueEntityValidator
line 120:

$entity === ($result instanceof \Iterator ? $result->current() : current($result))

current($result) returns false here, because the $result array is not rewound.

there is a rewind for \Iterator on line 112:

    if ($result instanceof \Iterator) {
        $result->rewind();
    }

so change it to

    if ($result instanceof \Iterator) {
        $result->rewind();
    }
    else if (is_array($result)) {
        reset($result);
    }
@fabpot fabpot added a commit that referenced this issue May 3, 2013
@fabpot fabpot merged branch xabbuh/issue6651 (PR #7906)
This PR was squashed before being merged into the 2.1 branch (closes #7906).

Discussion
----------

if the repository method returns an array ensure that it's internal poin...

...ter is resetted

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #6651
| License       | MIT
| Doc PR        |

Commits
-------

0610750 if the repository method returns an array ensure that it's internal poin...
795e828
@fabpot fabpot closed this May 3, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment