Thanks to visit codestin.com
Credit goes to github.com

Skip to content

PasswordHasherListener does not take into account of AbstractType #49260

Closed
@laurentmuller

Description

@laurentmuller

Symfony version(s) affected

6.2.5

Description

PasswordHasherListener class must take into account an AbstractType class when it's parent is a RepeatedType.

How to reproduce

I have created an AbstractType to use for repeat password:

class RepeatPasswordType extends AbstractType
{
    public function getParent(): ?string
    {
        return RepeatedType::class;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
         $resolver->setDefaults([
             'mapped' => false,
             'type' => PasswordType::class,
             // ....
             'first_options'  => ([
                 'hash_property_path' => 'password',
                 // ....
             ],             
         ]};
    }
}

When submitting the form, the PasswordHasherListener service cannot detect that my type is a RepeatedType class.

Possible Solution

Maybe we can create a function to detect this case:

private function isRepeatedType(?FormInterface $parentForm): bool
{
    if (null === $parentForm) {
        return false;
     }
     $innerType = $parentForm->getConfig()->getType()->getInnerType();

     return $innerType instanceof RepeatedType ||
            ($innerType instanceof AbstractType && RepeatedType::class === $innerType->getParent());
}

And use it in the registerPassword function:

public function registerPassword(FormEvent $event): void
{
    // ....
    if ($this->isRepeatedType($parentForm)) {
        $mapped = $parentForm->getConfig()->getMapped();
	$parentForm = $parentForm->getParent();
    }
    // ....
}

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions