Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 646 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is my Symfony2 @UniqueEntity constraint not working at all?

#1
I have the following entity class in my application:


<?php

namespace ...;

// use ...
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;;
// ...

/**
* @ORM\Table(name="sc_user")
* @ORM\Entity(repositoryClass="...\UserRepository")
* @ORM\HasLifecycleCallbacks()
* @UniqueEntity(fields={"email", "username"})
*/
class User implements UserInterface, \Serializable, EquatableInterface
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @var string $email
*
* @ORM\Column(name="email", type="string", length=255, unique=true)
*
* @Assert\NotBlank(groups={"registration"})
* @Assert\Email(groups={"registration"})
*/
private $email;

/**
* @var string $username
*
* @ORM\Column(name="username", type="string", length=32, unique=true)
*
* @Assert\NotBlank(groups={"registration"})
*/
private $username;

// ...
}

The @UniqueEntity constraint is being ignored. I tried different flavors including:

@UniqueEntity(fields={"email", "username"})

and

@UniqueEntity(fields={"email"})
@UniqueEntity(fields={"username"})

and, as per the Symfony2 documentation here: [

[To see links please register here]

][1]

@UniqueEntity("email")
@UniqueEntity("username")

Nothing I do, works. Instead of getting a form validation error as expected, I am getting the following exception:

> SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry
> '[email protected]' for key 'UNIQ_D8183973E7927C74'

This is just wrong! Does anyone know how to fix this issue?

[1]:

[To see links please register here]

Reply

#2
Try to add

framework:
validation:
enable_annotations: true

to your app configuration
Reply

#3
The problem was solved as follows:

@UniqueEntity(fields={"email"}, groups={"registration"})
@UniqueEntity(fields={"username"}, groups={"registration"})

The registration groups were missing, and I needed to separate them into two separate annotations.
Reply

#4
Please note ignoreNull

For example to work correctly with pid (can be null):

constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
fields: [page, pid, position]
errorPath: page
groups: [Menu]
message: "Page already exists with that parent"
ignoreNull: false

Reply

#5
Another reason for this issue is if you are using form collections and include a sub-form which handles an associated entity, you have to set `cascade_validation` to `true` in the root and all sub-forms.

See the hint in the [Symfony documentation](

[To see links please register here]

):
> To activate validation on CategoryType, add the `cascade_validation` option to `TaskType`:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\TaskBundle\Entity\Task',
'cascade_validation' => true,
));
}

**Update:**

To ensure that your child entities are validated there is even a better way. Symfony provides the `Valid` constraint for exactly this reason. From [documentation][1]:

> **Valid**
> This constraint is used to enable validation on objects that are embedded as properties on an object being validated. This allows you to validate an object and all sub-objects associated with it.


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through