Created
February 8, 2025 13:23
-
-
Save yceruto/0fe65c8669016fe48f24c4e047ce7fb1 to your computer and use it in GitHub Desktop.
Symfony Form Flow Expectation
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
{% extends 'base.html.twig' %} | |
{% block body %} | |
{{ form(form) }} | |
{% endblock %} |
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
<?php | |
namespace App\Controller; | |
use App\Form\Type\RegisterUserType; | |
use App\Model\UserDto; | |
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\Form\FormFlowFactoryInterface; | |
use Symfony\Component\Routing\Attribute\Route; | |
class RegisterUserController extends AbstractController | |
{ | |
public function __invoke(Request $request, FormFlowFactoryInterface $factory): Response | |
{ | |
$flow = $factory->create(RegisterUserType::class, new UserDto()); | |
$flow->handleRequest($request); | |
if ($flow->isSubmitted() && $flow->isValid() && $flow->isCompleted()) { | |
dd($flow->getData()); | |
} | |
return $this->render('form.html.twig', [ | |
'form' => $flow->getForm(), | |
]); | |
} | |
} |
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
<?php | |
namespace App\Form\Type; | |
use App\Model\UserDto; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\Extension\Core\Type\FormFlowType; | |
use Symfony\Component\Form\Extension\Core\Type\TextType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
class RegisterUserType extends AbstractType | |
{ | |
public function configureOptions(OptionsResolver $resolver): void | |
{ | |
$resolver->setDefaults([ | |
'steps' => [ | |
'personal' => UserPersonalType::class, | |
'professional' => UserProfessionalType::class, | |
'extra' => function (FormBuilderInterface $builder) { | |
$builder->add('position', TextType::class, ['required' => false]); | |
}, | |
'credential' => UserCredentialType::class, | |
], | |
'current_step_property_path' => 'currentStep', | |
'data_class' => UserDto::class, | |
]); | |
} | |
public function getParent(): string | |
{ | |
return FormFlowType::class; | |
} | |
} |
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
<?php | |
namespace App\Model; | |
use Symfony\Component\Validator\Constraints as Assert; | |
class UserDto | |
{ | |
// personal step | |
#[Assert\NotBlank(groups: ['personal'])] | |
public ?string $firstName = null; | |
public ?string $lastName = null; | |
#[Assert\GreaterThanOrEqual(18, groups: ['personal'])] | |
public ?int $age = null; | |
public bool $enterprise = false; | |
// professional step | |
#[Assert\NotBlank(groups: ['professional'])] | |
public ?string $company = null; | |
public ?string $position = null; | |
// credentials step | |
#[Assert\NotBlank(groups: ['credential'])] | |
#[Assert\Email(groups: ['credential'])] | |
public ?string $email = null; | |
#[Assert\NotBlank(groups: ['credential'])] | |
#[Assert\PasswordStrength(minScore: Assert\PasswordStrength::STRENGTH_WEAK, groups: ['credential'])] | |
public ?string $plainPassword = null; | |
public string $currentStep = 'personal'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment