0

I want to display error message in symfony instead of exception when user put duplicate name. Now it's not work and symfony displays exception.

/**
 * Download
 *
 * @ORM\Table(name="izo_download")
 * @ORM\Entity(repositoryClass="AppBundle\Entity\DownloadRepository")
 * @UniqueEntity(
 *     fields={"name"},
 *     message="This name exist."
 * )
 * @Vich\Uploadable
 */
class Download{
/**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255, unique=true)
     */
    private $name;

Error message:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'izoplast-lato.txt' for key 'UNIQ_429C16C25E237E06'

What I do wrong?

2
  • What happens if you change fields={"name"} to fields="name" ? Also I guess you added use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; reference as well. Commented Jul 17, 2015 at 22:04
  • Look at this example. inanzzz.com/index.php/post/d3ne/… Commented Jul 17, 2015 at 22:06

1 Answer 1

1

Try with replacing fields={"name"} with fields="name". It works for me. I've just tried. Also check this out.

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

/**
 * @ORM\Table(name="izo_download")
 * @UniqueEntity(fields="name", message="This name exist.")
 */
class Download
{
    /**
     * @ORM\Column(name="name", type="string", length=255, unique=true)
     */
    private $name;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for reply. I have replaced fields={"name"} to fields="name", but it still throws exception ;/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.