表单提交:值设置为空
Form submission : value is set on null
我正在制作一个广告平台,我刚刚创建了一个 Booking 实体及其表单,但是在提交表单之后,值 'amount' 设置为 null,但它不应该为 null。
我创建了一个 prePersist 函数来设置刷新前的数量 属性。
这是我的实体 Booking
中的 prePersist 函数
* @ORM\PrePersist
*
* @return void
*/
public function prePersist()
{
if(empty($this->createdAt))
{
$this->createdAt = new \DateTime();
}
if(empty($this->amount))
{
$this->amount = $this->ad->getPrice() * $this->getDuration();
}
}
public function getDuration()
{
$diff = $this->endDate->diff($this->startDate);
return $this->days;
}
我的预订控制器
/**
* @Route("/annonces/{id}/booking", name="ad_booking")
* @IsGranted("ROLE_USER")
*/
public function booking(Ad $ad, Request $request, ObjectManager $manager)
{
$booking = new Booking;
$form = $this->createForm(BookingType::class, $booking);
$user = $this->getUser();
$booking->setBooker($user)
->setAd($ad);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$manager->persist($booking);
$manager->flush();
return $this->redirectToRoute('booking_success', [
'id' => $booking->getId()
]);
}
return $this->render('booking/booking.html.twig', [
'ad' => $ad,
'bookingForm' => $form->createView()
]);
}
}
当用$this->getUser()定义用户时,它不起作用;在提交和有效性检查中。自从我开始学习 Symfony 以来,这是第一次发生这种情况。我确定我一定是忘记了什么,但是我花了很多时间思考什么,以至于我看不到答案。
和我的 BookingType 表格
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('startDate', DateType::class, [
'label' => 'Date de début de prestatation',
'widget' => 'single_text'
])
->add('endDate', DateType::class, [
'label' => 'Date de fin de prestatation',
'widget' => 'single_text'
])
;
}
提交表单时,应该调用prePersist函数,然后设置金额属性,但returns为空。我真的不明白我错过了什么。
因为你的 PrePersist
似乎没有被触发,我猜你可能忘记了实体上的 @ORM\HasLifecycleCallbacks()
注释。
/**
* @ORM\Entity(repositoryClass="App\Repository\BookingRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Booking
{
...
}
我刚刚发现那是错误的。它链接到自动验证:
在 validator.yaml
我用
评论了 auto_mapping
App\Entity\Booking: true
App\Entity\User: true
现在一切正常!
我正在制作一个广告平台,我刚刚创建了一个 Booking 实体及其表单,但是在提交表单之后,值 'amount' 设置为 null,但它不应该为 null。
我创建了一个 prePersist 函数来设置刷新前的数量 属性。
这是我的实体 Booking
中的 prePersist 函数 * @ORM\PrePersist
*
* @return void
*/
public function prePersist()
{
if(empty($this->createdAt))
{
$this->createdAt = new \DateTime();
}
if(empty($this->amount))
{
$this->amount = $this->ad->getPrice() * $this->getDuration();
}
}
public function getDuration()
{
$diff = $this->endDate->diff($this->startDate);
return $this->days;
}
我的预订控制器
/**
* @Route("/annonces/{id}/booking", name="ad_booking")
* @IsGranted("ROLE_USER")
*/
public function booking(Ad $ad, Request $request, ObjectManager $manager)
{
$booking = new Booking;
$form = $this->createForm(BookingType::class, $booking);
$user = $this->getUser();
$booking->setBooker($user)
->setAd($ad);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$manager->persist($booking);
$manager->flush();
return $this->redirectToRoute('booking_success', [
'id' => $booking->getId()
]);
}
return $this->render('booking/booking.html.twig', [
'ad' => $ad,
'bookingForm' => $form->createView()
]);
}
}
当用$this->getUser()定义用户时,它不起作用;在提交和有效性检查中。自从我开始学习 Symfony 以来,这是第一次发生这种情况。我确定我一定是忘记了什么,但是我花了很多时间思考什么,以至于我看不到答案。
和我的 BookingType 表格
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('startDate', DateType::class, [
'label' => 'Date de début de prestatation',
'widget' => 'single_text'
])
->add('endDate', DateType::class, [
'label' => 'Date de fin de prestatation',
'widget' => 'single_text'
])
;
}
提交表单时,应该调用prePersist函数,然后设置金额属性,但returns为空。我真的不明白我错过了什么。
因为你的 PrePersist
似乎没有被触发,我猜你可能忘记了实体上的 @ORM\HasLifecycleCallbacks()
注释。
/**
* @ORM\Entity(repositoryClass="App\Repository\BookingRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Booking
{
...
}
我刚刚发现那是错误的。它链接到自动验证:
在 validator.yaml
我用
auto_mapping
App\Entity\Booking: true
App\Entity\User: true
现在一切正常!