Forms 所有视图中的Symfony访问表单
我在应用程序中有一个登录/注册表单Forms 所有视图中的Symfony访问表单,forms,symfony,Forms,Symfony,我在应用程序中有一个登录/注册表单 namespace UserBundle\Form; use Symfony\Component\Form\AbstractType; class RegisterType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('username',
namespace UserBundle\Form;
use Symfony\Component\Form\AbstractType;
class RegisterType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('username', 'text', ['label' => 'Username']);
$builder->add('email', 'hidden', ['label' => 'Email']);
$builder->add('password', 'password',['label' => 'Password']);
$builder->add('cancel', SubmitType::class, [
'label' => 'CANCEL'
]);
$builder->add('register', SubmitType::class, [
'label' => 'CREATE ACCOUNT'
]);
}
public function getName()
{
return 'register';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults([
'data_class' => 'UserBundle\Entity\User',
]);
}
}
我希望这在所有页面中都可用,因此我希望表单标记放置在base.html.twig
我的问题是,Symfony推荐的方法是什么?我不想在控制器的每一个动作中都传递表单
我怎么知道的?通过定义服务?欢迎提供任何示例或指针
谢谢。您的意思是传递由您的
注册表类型创建的FormView
的实例
我认为最简单的方法是创建Twig扩展,它将FormBuilder
注册为它的依赖项,并在表单视图中公开一个函数(甚至可能是一个全局变量)。然后,您还可以只使用表单视图的一个实例,并根据需要多次使用它
请参见我需要一个dedictaed控制器来处理表单。
然后可以直接从twig调用此控制器操作,将控制器调用添加到main layout.html.twig中
例如
控制器是这样的
class LoginController extends Controller
{
public function form(Request $request)
{
// create your form
// return html of form
}
}
参考:-看@Blowski的答案
注册示例
<?php
namespace AppBundle\Controller\Security;
use AppBundle\Entity\User,
AppBundle\Form\Entity\UserType;
use \Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller,
Symfony\Component\HttpFoundation\Request;
class RegistrationController extends Controller
{
/**
* @Route("/register", name="user_registration")
*/
public function registerAction(Request $request)
{
// 1) build the form
$user = new User();
$form = $this->createForm(UserType::class, $user);
// 2) handle the submit (will only happen on POST)
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// 3) Encode the password (you could also do this via Doctrine listener)
$password = $this->get('security.password_encoder')
->encodePassword($user, $user->getPlainPassword());
$user->setPassword($password);
// 4) save the User!
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
// ... do any other work - like sending them an email, etc
// maybe set a "flash" success message for the user
return $this->redirectToRoute('replace_with_some_route');
}
return $this->render(
'AppBundle:security:register.html.twig',
array('form' => $form->createView())
);
}
}
假设我也希望将其用于注册,那么我们在哪里处理注册逻辑呢?我只需要创建一个控制器,例如RegistrationController,它具有处理用户注册的功能。然后可以使用细枝控制器函数调用所需的操作。将更新答案谢谢,我忘了提到它是Ajax表单。不管怎样,我现在知道了该怎么走了。:-)
<?php
namespace AppBundle\Controller\Security;
use AppBundle\Entity\User,
AppBundle\Form\Entity\UserType;
use \Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller,
Symfony\Component\HttpFoundation\Request;
class RegistrationController extends Controller
{
/**
* @Route("/register", name="user_registration")
*/
public function registerAction(Request $request)
{
// 1) build the form
$user = new User();
$form = $this->createForm(UserType::class, $user);
// 2) handle the submit (will only happen on POST)
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// 3) Encode the password (you could also do this via Doctrine listener)
$password = $this->get('security.password_encoder')
->encodePassword($user, $user->getPlainPassword());
$user->setPassword($password);
// 4) save the User!
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
// ... do any other work - like sending them an email, etc
// maybe set a "flash" success message for the user
return $this->redirectToRoute('replace_with_some_route');
}
return $this->render(
'AppBundle:security:register.html.twig',
array('form' => $form->createView())
);
}
}