-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
InputBag::get can return array, too #45650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Just a simple fix to help static code analysis. Simply using a form will trigger a returned array when calling `::get()` on the form name, for instance, containing all the keys.
it can not ;) |
Then why do I have one? Am I a true magician? ;)
<?php
declare(strict_types = 1);
namespace App\Form\Admin;
use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Mapping\ClassMetadata;
class UserPasswordType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add(
'oldpassword',
PasswordType::class,
[
'label' => 'front.users.forms.password.oldpassword.label',
'mapped' => false,
'required' => true,
'empty_data' => '',
'invalid_message' => 'front.users.forms.password.invalid_repeated',
'help' => 'front.users.forms.password.oldpassword.help',
]
)
->add(
'password',
RepeatedType::class,
[
'type' => PasswordType::class,
'required' => true,
'empty_data' => '',
'options' => ['attr' => ['class' => 'password-field']],
'first_options' => [
'label' => 'front.users.forms.password.first',
'attr' => [
'minlength' => '6',
],
'help' => 'front.users.forms.password.help',
],
'second_options' => [
'label' => 'front.users.forms.password.second',
'attr' => [
'minlength' => '6',
],
'help' => 'front.users.forms.password.help',
],
'invalid_message' => 'front.users.forms.password.invalid_repeated',
'help' => 'front.users.forms.password.help',
'constraints' => [
new Length([
'min' => 6,
]),
],
]
);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
public static function loadValidatorData(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint(
'oldpassword',
new UserPassword([
'message' => 'front.users.forms.password.oldpassword.error',
])
);
}
}
<?php
declare(strict_types = 1);
namespace App\Controller\Front;
use App\Entity\User;
use App\Form\Admin\UserPasswordType;
use App\Form\Front\UserType;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
#[Route('/my-account')]
#[Security("is_granted('ROLE_PARTICIPANT')")]
class AccountController extends AbstractController
{
#[Route('/edit-my-password', name: 'my_account_password', methods: ['GET', 'POST'])]
public function passwordEdit(Request $request, TranslatorInterface $translator, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $em): Response
{
/** @var User $user */
$user = $this->getUser();
$form = $this->createForm(UserPasswordType::class, $user);
$form->handleRequest($request);
if (
$form->isSubmitted()
&& $form->isValid()
&& ! empty($request->request->get('user_password')['oldpassword'])
&& ! empty($request->request->get('user_password')['password']['first'])
) {
$user->setPassword($userPasswordHasher->hashPassword($user, $request->request->get('user_password')['password']['first']));
$em->persist($user);
$em->flush();
$this->addFlash('success', $translator->trans('front.users.forms.password.success', ['{entity_name}' => $user->getDisplayName()]));
return $this->redirectToRoute('my_account', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('front/account/my_account_password.html.twig', [
'user' => $user,
'form' => $form,
]);
}
... |
perhaps check your deprecations on 5.4
|
Duplicate of #41766 |
Thanks! Got it, now! Totally missed that part! :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Just a simple fix to help static code analysis. Simply using a form will trigger a returned array when calling
::get()
on the form name, for instance, containing all the keys.This doesn't break test, as it only changes PHPDoc.
6.1 only (not going back to 5.4 so as not to break static code analysis (I suppose this is better as it's also absolutely not critical).