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.