gpt4 book ai didi

Symfony - 如何小写表单字段?

转载 作者:行者123 更新时间:2023-12-04 14:20:00 25 4
gpt4 key购买 nike

我是 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' ))

如果你需要看我的 Controller :

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(),
));
}

感谢您的帮助!

最佳答案

如果您使用的是 Bootstrap ,则以下代码应该可以工作:

$builder->add('name', TextType::class, ['attr' => array( 'class' => 'text-lowercase' ))

关于Symfony - 如何小写表单字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56038806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com