Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some Documentation for UniqueEntity constraint #873

Closed
keymaster opened this issue Nov 23, 2011 · 4 comments
Closed

Some Documentation for UniqueEntity constraint #873

keymaster opened this issue Nov 23, 2011 · 4 comments

Comments

@keymaster
Copy link

The following should be added:

First, you also need to add a use statement to your entity as follows:

use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

Second, in your xml file, the constraint is specified a little differently than other constraints. The constraint tag is not placed within the property tag, but is right under the class tag, at the same level as the property tag.

Third, in your constraint tag, you need to specify the full pathname to the constraint . You can't just say UniqueEntity.

Finally, if you want multiple fields within the entity to be unique, you need separate constraint tags.

Here is an example:

<class name="Acme\WidgetsBundle\Entity\BlackWidget">
        <constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
            <option name="fields">email</option>
            <option name="message">This email already exists.</option>
        </constraint>
        <constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
            <option name="fields">phone</option>
            <option name="message">This phone number already exists.</option>
        </constraint>
         <constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
            <option name="fields">website</option>
            <option name="message">This website already exists.</option>
        </constraint>
         <property name="email">
            <constraint name="Email" />
        </property>
    </class>
@weaverryan
Copy link
Member

I've added some clarifications and fixes per your notes here. Can you take a look at them and see if you can see any issues?

Thanks!

@keymaster
Copy link
Author

Looks good.

You may want to add a short blurb explaining that to get unique validation on multiple fields in the entity, you need to replicate the entire constraint tag block, once for each field.

@mrtorrent
Copy link
Contributor

Yes, the wording of the explanation for the "fields" parameter is a bit ambiguous

you could specify that both the email and name fields in the User example above should be unique.

To me, this sounds like the fields you pass to a single UniqueEntity statement will separately be checked for uniqueness in the table, rather than as combination, which I believe is what happens.

@weaverryan
Copy link
Member

@mrtorrent I agree!

I've just made this language much more clear.

Thanks guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants