Skip to content

Doctrine UniqueEntity Contraint allow specify the entity. #14917

Closed
@desarrolla2

Description

@desarrolla2
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!

Activity

webmozart

webmozart commented on Jun 10, 2015

@webmozart
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

desarrolla2 commented on Jun 10, 2015

@desarrolla2
ContributorAuthor

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

ogizanagi commented on Jun 14, 2015

@ogizanagi
Contributor

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

desarrolla2 commented on Jun 15, 2015

@desarrolla2
ContributorAuthor

@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

kbond commented on Jun 23, 2015

@kbond
Member

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

qpautrat

qpautrat commented on Mar 1, 2016

@qpautrat

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

MatTheCat

MatTheCat commented on Mar 30, 2017

@MatTheCat
Contributor

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

HeahDude

HeahDude commented on Jul 30, 2017

@HeahDude
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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @kbond@webmozart@desarrolla2@qpautrat@MatTheCat

        Issue actions

          Doctrine UniqueEntity Contraint allow specify the entity. · Issue #14917 · symfony/symfony