0

I have a Book entity with Author relationship:

/**
 * Book
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="App\BookBundle\Entity\BookRepository")
 */
class Book {

    /**
     * @var integer
     *
     * @ORM\Column(name="author_id", type="integer")
     * 
     */
    private $authorId;

    /**
     * 
     * @ORM\ManyToOne(targetEntity="Author", inversedBy="books")
     * @ORM\JoinColumn(name="author_id", referencedColumnName="id")
     */
    private $author;

    //the rest of entity...

}

/**
 * Author
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="App\BookBundle\Entity\AuthorRepository")
 */
class Author {

    /**
     * @ORM\OneToMany(targetEntity="Book", mappedBy="author")
     */
    private $books;

    //the rest of entity...

}

And I have a form BookType containing author_id field.

  class BookType extends AbstractType {

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options) {     
         $builder->add('authorId', 'hidden', array(
            ))
         //the rest of the form...

The author_id input is hidden and its value is filled with autocomplete feature on client-side. Now I need to validate it on server-side when the form is submitted.

How to validate that author for given author_id exists? I can't believe that there's no built-in solution for that in Symfony/Doctrine.

Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64
  • 2
    Check the [GregwarFormBundle](https://github.com/Gregwar/FormBundle). Hope this help – Matteo Sep 28 '14 at 12:18
  • Thanks Matteo. Seems to be exactly what I need. The only thing now is that I don't know how to change the default validation error message while providing non existing `author_id`. I didn't find anything about its validation. Have you any idea? – Jakub Matczak Sep 28 '14 at 15:21
  • Try adding a translation for the error message ("Can not find entity" I suppose) in a validation.yml or validation.xml files in accordion to standard form validation translation messages. – Matteo Sep 28 '14 at 15:36

0 Answers0