【发布时间】:2017-03-29 01:21:34
【问题描述】:
我正在使用一个实体,而这个实体继承自另一个实体。除了一件事,一切都很好。似乎基础实体字段上的服务器端验证“丢失”了。
继承类:
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* TicketBase
* @ORM\MappedSuperclass
*/
class TicketBase
{
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
* @Assert\NotBlank()
*/
private $title;
/* other fields */
然后:
use AppBundle\Repository\TicketRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* Ticket
*
* @ORM\Table(name="cheval_ticket")
* @ORM\Entity(repositoryClass="AppBundle\Repository\TicketRepository")
*/
class Ticket extends TicketBase
{
/* only specific fields here */
之后,我可以在 Ticket 实体中使用我的任何 TicketBase 字段没有问题,但是当我在 Ticket 上创建表单时,我在 Ticket::title 上没有服务器端验证,因此如果出现完整性约束违规标题为空。
我是否遗漏了一些东西来让我的验证工作?
谢谢
编辑:
控制器动作:
/**
* @Route("/tickets/{uniqid}/{contactId}", name="contact_tickets")
*/
public function ticketsAction(Request $request, $contactId = null, $uniqid = null)
{
$em = $this->getDoctrine()->getManager();
$contact = $em->getRepository("AppBundle:Contact")
->findOneBy(array(
'id' => $contactId,
'uniqid' => $uniqid
));
if ($contact === null) {
return $this->go('contact_index');
}
$form = $this
->createForm(ContactTicketsType::class, $contact, ['method' => 'POST'])
->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->setTicketsPrice($contact);
$this->setTicketsNbJours($contact);
$this->save($contact);
$this->setTicketsCodeId($contact);
return $this->go('payement_index', [
'contactId' => $contact->getId(),
'uniqid' => $contact->getUniqid(),
]
);
}
}
我的联系实体与 Ticket 有这种关系:
/**
* @ORM\OneToMany(targetEntity="Ticket", mappedBy="contact", cascade={"remove", "persist"})
*/
private $tickets;
还有我的 ContactTicketsType :
namespace AppBundle\Form\Type\Contact;
use AppBundle\Form\Type\Ticket\TicketType;
use AppBundle\Form\Type\Ticketadd\TicketaddType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Description of ContactType
*/
class ContactTicketsType extends AbstractType
{
/**
*
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('tickets', CollectionType::class, array(
'entry_type' => TicketType::class,
'allow_add' => true,
'allow_delete' => true,
'label' => false,
'by_reference' => false
))
// ...
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Contact'
));
}
}
【问题讨论】:
标签: inheritance constraints symfony