我是使用Symfony 3.4的初学者,我想将表单字段更改为小写,但我不知道如何或在何处:( 在我的带有约束的buildForm中或在Controller中,但是我无法定位表单字段?
我在Twig中尝试过
<div>{{ form_widget(form.name)|lower }}</div>
我在Controller中尝试过
$form->get('name')->setData(strtolower($form->get('name')));
我在buildForm中尝试过
$builder->add('name', TextType::class, ['attr' => array( 'class' => 'text-lowercase' ))
如果您需要查看我的控制器:
public function registerAction(Request $request)
{
/** @var $formFactory FactoryInterface */
$formFactory = $this->get('fos_user.registration.form.factory');
/** @var $userManager UserManagerInterface */
$userManager = $this->get('fos_user.user_manager');
/** @var $dispatcher EventDispatcherInterface */
$dispatcher = $this->get('event_dispatcher');
$user = new User();
$user->setEnabled(true);
$event = new GetResponseUserEvent($user, $request);
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
$form = $formFactory->createForm();
$user->setUsername("null");
$user->setPassword("null");
$user->setPlainPassword("null");
$form->setData($user);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$user->setPassword(strtolower($form["name"]->getData(). $form["firstname"]->getData()));
$user->setPlainPassword(strtolower($form["name"]->getData(). $form["firstname"]->getData()));
$user->setUsername(strtolower($form["name"]->getData().
$form["firstname"]->getData()));
if($form["roles"]->getData() == 'ROLE_ADMIN')
{
$user->addRole('ROLE_ADMIN');
}
else
{
$user->addRole('ROLE_USER');
}
$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);
$userManager->updateUser($user);
/*****************************************************
* Add new functionality (e.g. log the registration) *
*****************************************************/
$this->container->get('logger')->info(
sprintf("New user registration: %s", $user)
);
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('fos_user_registration_register');
$response = new RedirectResponse($url);
}
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_FAILURE, $event);
if (null !== $response = $event->getResponse()) {
return $response;
}
}
return $this->render('@FOSUser/Registration/register.html.twig', array(
'form' => $form->createView(),
));
}
谢谢您的帮助!
答案 0 :(得分:3)
如果您正在使用引导程序,则应执行以下代码:
$builder->add('name', TextType::class, ['attr' => array( 'class' => 'text-lowercase' ))