Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
You need to fill the object that you pass to the form with the necessary data:
$post = new Post();
// Заполняем поля, которые присутствуют в форме
$post->setPublishedAt(new \DateTime());
$post->setTitle('New title');
// Если объекты не используются, то заполнять надо массив
// $post = [];
// $post['publishedAt'] = new \DateTime();
// $post['title'] = 'New title';
$form = $this->createForm(PostType::class, $post);
// ...
return $this->render('post.html.twig', [
'form' => $form->createView(),
])
$builder->get('publishedAt')->setData(new \DateTime());
$builder->get('title')->setData('New title');
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$data = $event->getData();
if ($data !== null) {
return;
}
$event->setData([
'title' => 'New title',
'publishedAt' => new \DateTime(),
]);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question