我正在为自己的评估编写一个简单的停车场应用程序,我需要做的最后一件事是在重定向用户数据后将其保留在表单字段中。重定向发生在用户要保留的地点已被占用时。
我发现您可以使用会话来执行此操作,但是由于我无法检索数据,因此它不能以我想要的方式工作。
这是我的预订控制器中的新方法:
/**
* New action.
*
* @param \Symfony\Component\HttpFoundation\Request $request HTTP request
* @param \App\Repository\ReservationRepository $repository Reservation repository
*
* @return \Symfony\Component\HttpFoundation\Response HTTP response
*
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*
* @Route(
* "/new",
* methods={"GET", "POST"},
* name="reservation_new",
* )
*/
public function new(Request $request, ReservationRepository $repository): Response
{
$reservation = new Reservation();
$form = $this->createForm(ReservationType::class, $reservation);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($repository->findOneBy(['spot' => $reservation->getSpot()])) {
// $occupied_from = $form['occupied_from']->getData();
// $occupied_to = $form['occupied_to']->getData();
// $payment = $form['payment']->getData();
// $spot = $form['spot']->getData();
dump($request->request->get('reservation'));
// $occupied_from = $this->session->get('occupied_from', []);
// $occupied_to = $this->session->get('occupied_to', []);
// $payment = $this->session->get('payment', []);
// $spot = $this->session->get('spot', []);
// if ($reservation->getOccupiedFrom() == )
$this->addFlash('error', 'message.spot_taken');
return $this->redirectToRoute('reservation_new');
}
$reservation->setClient($this->getUser());
$repository->save($reservation);
$this->addFlash('success', 'message.updated_successfully');
return $this->redirectToRoute('reservation_index');
}
if ($form->isSubmitted() && $form->isValid() && $reservation->getOccupiedFrom() < new \DateTime('now') ) {
$this->addFlash('error', 'message.inconsistent_date');
}
if ($form->isSubmitted() && $form->isValid() && $reservation->getOccupiedFrom() > $reservation->getOccupiedTo()) {
$this->addFlash('error', 'message.inconsistent_date');
return $this->redirectToRoute('reservation_new');
}
return $this->render(
'reservation/new.html.twig',
['form' => $form->createView()]
);
}
这是预订表格:
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add(
'occupiedFrom',
DateTimeType::class,
[
'widget' => 'choice',
'label' => 'label.occupied_from',
'placeholder' => [
'year' => 'Year', 'month' => 'Month', 'day' => 'Day',
'hour' => 'Hour', 'minute' => 'Minute', 'second' => 'Second',
],
'data' => new\DateTime(),
'attr' => ['min' => (new DateTime())->format('YYYY-mm-dd HH:ii:ss')]
]
);
$builder->add(
'occupiedTo',
DateTimeType::class,
[
'widget' => 'choice',
'label' => 'label.occupied_to',
'placeholder' => [
'year' => 'Year', 'month' => 'Month', 'day' => 'Day',
'hour' => 'Hour', 'minute' => 'Minute', 'second' => 'Second',
],
'data' => new\DateTime(),
'attr' => ['min' => (new DateTime())->format('YYYY-mm-dd HH:ii:ss')]
]
);
$builder->add(
'payment',
EntityType::class,
[
'class' => Payment::class,
'choice_label' => function ($payment) {
return $payment->getType();
},
'label' => 'label.payment',
'placeholder' => 'label.none',
'required' => true,
]
);
$builder->add(
'spot',
EntityType::class,
[
'class' => Spot::class,
'choice_label' => function($spot) {
return $spot->getSpot();
},
'label' => 'label.spot',
'placeholder' => 'label.none',
'required' => true,
]
);
我需要访问4个值:'occupied_from'是预订的开始日期,'occupied_to'是预订的日期,'pay'是预订的两种付款方式之一,'spot'是是景点的名称。
无论我做什么,我总是会得到重定向,但不会保存数据。预先感谢没有经验的同事。
答案 0 :(得分:0)
您可以将数据发送到路线,如下所示:
vec1 *= vec1 >= 0.5
vec2.reshape(-1, 3)[:] = vec1.reshape(-1, 1)
vec2 *= vec3
无论如何,您可以在任何控制器或服务中使用SessionInterface组件,如下所示:
在控制器中(如@msg在下面的注释中建议):
return $this->redirectToRoute(
'reservation_new',
[
'occupied_to' => $occupiedTo,
//etc ...
],
301
);
//in the other side:
public function reservationNew(string $occupiedTo) // ...
或在服务中
$session = $this->get('session');
之后,您可以使用optionsResolver重建表单,或者知道您想要的是什么