Skip to content

Conversation

DocFX
Copy link
Contributor

@DocFX DocFX commented Mar 5, 2022

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.

Q A
Branch? 6.1 (mostly not to break previous code analysis)
Bug fix? no
New feature? no
Deprecations? no
Tickets
License MIT
Doc PR

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).

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.
@carsonbot carsonbot added this to the 6.1 milestone Mar 5, 2022
@ro0NL
Copy link
Contributor

ro0NL commented Mar 6, 2022

InputBag::get can return array

it can not ;)

@DocFX
Copy link
Contributor Author

DocFX commented Mar 6, 2022

Then why do I have one? Am I a true magician? ;)

src/Form/Admin/UserPasswordType.php

<?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',
            ])
        );
    }
}

src/Controller/Front/AccountController.php

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

   ...

@ro0NL
Copy link
Contributor

ro0NL commented Mar 6, 2022

perhaps check your deprecations on 5.4

trigger_deprecation('symfony/http-foundation', '5.1', 'Retrieving a non-string value from "%s()" is deprecated, and will throw a "%s" exception in Symfony 6.0, use "%s::all($key)" instead.', __METHOD__, BadRequestException::class, __CLASS__);

@nicolas-grekas
Copy link
Member

Duplicate of #41766

@nicolas-grekas nicolas-grekas marked this as a duplicate of #41766 Mar 6, 2022
@derrabus derrabus closed this Mar 6, 2022
@DocFX DocFX deleted the patch-4 branch March 6, 2022 21:52
@DocFX
Copy link
Contributor Author

DocFX commented Mar 6, 2022

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants