Symfony-ux-live-comment Не найдено соответствующего свойства «form» или аргумента mount() ⇐ Php
-
Anonymous
Symfony-ux-live-comment Не найдено соответствующего свойства «form» или аргумента mount()
class PostForm extends AbstractController
{
use DefaultActionTrait;
use ComponentWithFormTrait;
/**
* The initial data used to create the form.
*/
#[LiveProp]
public ?Post $initialFormData = null;
protected function instantiateForm(): FormInterface
{
// we can extend AbstractController to get the normal shortcuts
return $this->createForm(PostType::class, $this->initialFormData);
}
#[LiveAction]
public function save(EntityManagerInterface $entityManager)
{
// Submit the form! If validation fails, an exception is thrown
// and the component is automatically re-rendered with the errors
$this->submitForm();
/** @var Post $post */
$post = $this->getForm()->getData();
$entityManager->persist($post);
$entityManager->flush();
$this->addFlash('success', 'Post saved!');
return $this->redirectToRoute('app_post_show', [
'id' => $post->getId(),
]);
}
}
class Post
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $title = null;
#[ORM\Column(length: 255)]
private ?string $slug = null;
#[ORM\Column(type: Types::TEXT)]
private ?string $content = null;
..........
}
class PostType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('title')
->add('slug')
->add('content')
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Post::class,
]);
}
}
class PostController extends AbstractController
{
#[Route('/post', name: 'app_post')]
public function index(): Response
{
return $this->render('post/index.html.twig', [
'controller_name' => 'PostController',
'form' => $this->createForm(PostType::class, new Post())
]);
}
#[Route('/admin/post/{id}/edit', name: 'app_post_edit')]
public function edit(Request $request, Post $post, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(PostType::class, $post);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// save, redirect, etc
}
return $this->render('post/edit.html.twig', [
'post' => $post,
'form' => $form->createView(), // use $form->createView() in Symfony
Подробнее здесь: https://stackoverflow.com/questions/789 ... ent-was-fo
class PostForm extends AbstractController
{
use DefaultActionTrait;
use ComponentWithFormTrait;
/**
* The initial data used to create the form.
*/
#[LiveProp]
public ?Post $initialFormData = null;
protected function instantiateForm(): FormInterface
{
// we can extend AbstractController to get the normal shortcuts
return $this->createForm(PostType::class, $this->initialFormData);
}
#[LiveAction]
public function save(EntityManagerInterface $entityManager)
{
// Submit the form! If validation fails, an exception is thrown
// and the component is automatically re-rendered with the errors
$this->submitForm();
/** @var Post $post */
$post = $this->getForm()->getData();
$entityManager->persist($post);
$entityManager->flush();
$this->addFlash('success', 'Post saved!');
return $this->redirectToRoute('app_post_show', [
'id' => $post->getId(),
]);
}
}
class Post
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $title = null;
#[ORM\Column(length: 255)]
private ?string $slug = null;
#[ORM\Column(type: Types::TEXT)]
private ?string $content = null;
..........
}
class PostType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('title')
->add('slug')
->add('content')
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Post::class,
]);
}
}
class PostController extends AbstractController
{
#[Route('/post', name: 'app_post')]
public function index(): Response
{
return $this->render('post/index.html.twig', [
'controller_name' => 'PostController',
'form' => $this->createForm(PostType::class, new Post())
]);
}
#[Route('/admin/post/{id}/edit', name: 'app_post_edit')]
public function edit(Request $request, Post $post, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(PostType::class, $post);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// save, redirect, etc
}
return $this->render('post/edit.html.twig', [
'post' => $post,
'form' => $form->createView(), // use $form->createView() in Symfony
Подробнее здесь: https://stackoverflow.com/questions/789 ... ent-was-fo
Мобильная версия