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

Doctrine UniqueEntity Contraint allow specify the entity. #14917

Closed
desarrolla2 opened this issue Jun 9, 2015 · 8 comments
Closed

Doctrine UniqueEntity Contraint allow specify the entity. #14917

desarrolla2 opened this issue Jun 9, 2015 · 8 comments

Comments

@desarrolla2
Copy link
Contributor

This constraint was thought-out to be used directly on a entity. But in several cases, i used it without a doctrine entity, for example when the form has fields for diferents entities.

In this case i want to have the way to specify not only the field also the entity that belongs the field.

I think that the change will be easy, so i will send a PR soon.

I think that the PR will be on 2.8 it is?

Regards!

@webmozart
Copy link
Contributor

Hi @desarrolla2, thank you for suggesting this feature! Can you give an example when this would be useful? I don't quite understand what you propose.

@desarrolla2
Copy link
Contributor Author

I will tell you my current use case.

I have 2 entities: User that extends FOSUser and Profile that contains firstName, lastName, Address, and others.

Now for a current promotion, i need to create a landing page with some fields from entity User and some fields from entity Profile.

Then i need create a model with this form:

<?php
namespace Sod\WebBundle\Form\Model;

use Symfony\Component\Validator\Constraints as Assert;
/** Here are my current implementation **/
use Sod\WebBundle\Form\Validator\Constraint\UniqueField;

/**
 * LandingRegistrationModel
 */
class LandingRegistrationModel
{
    /**
     * @Assert\NotBlank()
     * @Assert\Length( min=3, max=30 )
     * @UniqueField(entity="CoreBundle:User", field="username", fieldName="nombre de usuario")
     */
    public $userName;

    /**
     * @Assert\NotBlank()
     * @Assert\Length( min=3, max=300 )
     * @Assert\Email()
     * @UniqueField(entity="CoreBundle:User", field="email", fieldName="email")
     */
    public $email;

    /**
     * @Assert\NotBlank()
     * @Assert\Length( min=3, max=30 )
     */
    public $plainPassword;

    /**
     * @Assert\NotBlank()
     * @Assert\Length( min=3 )
     */
    public $firstName;

    /**
     * @Assert\NotBlank()
     * @Assert\Length( min=3 )
     */
    public $lastName;

    /**
     * @Assert\NotBlank()
     * @Assert\Length( min=3 )
     */
    public $city;

    /**
     * @Assert\NotBlank()
     * @Assert\Length( min=3 )
     * @Assert\Regex("/^[0-9]{2,3}-? ?[0-9]{6,7}$/")
     */
    public $phone;
}

later in controller y will use it well

<?php

$form = $this->createForm(new LandingRegistrationType(), new LandingRegistrationModel());

@ogizanagi
Copy link
Member

ogizanagi commented Jun 14, 2015

I would have said that this PR will indirectly allow you to do such a thing, but it's actually more complex:

I think it's a very particular use case and it does not belong to the UniqueEntity constraint responsibility, because you're applying this constraint on a non-entity object as an intermediate. IMO you'll need your own custom constraint.
Why do you need this intermediate object in the first place ? Is there anything preventing you to define a form with appropriated fields, relying on both Profile and FOSUser entities ? You don't need an intermediate model for this.

Sorry if I misunderstood your use case.

EDIT: After reading the following comments and having experienced this myself, I fully agree we need something like this.

@desarrolla2
Copy link
Contributor Author

@ogizanagi sorry if I did not explain properly, but I think using intermediate objects for form validation is the right use.

If you always use your entities to validate forms, there are many cases where you can be restricted, here are some examples.

1- complex forms that need several entities.
2- validations require different forms depending on the part of the application in which you are.

@kbond
Copy link
Member

kbond commented Jun 23, 2015

I also would like this feature. I do my validation on DTO's rather then on the entities themselves.

@qpautrat
Copy link

qpautrat commented Mar 1, 2016

👍
This is useful in CQRS context.
I validate my commands. But I have to create a custom UniqueEntity validator like @desarrolla2 did.

@MatTheCat
Copy link
Contributor

Any news on this? I thought entityClass addressed this issue but I was mistaken.

@HeahDude
Copy link
Contributor

I suggest to close this old issue either as duplicate of #22592 or fixed by #15002.

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

No branches or pull requests

7 participants