-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from core23/form-type
Replace hardcoded forms with symfony forms
- Loading branch information
Showing
8 changed files
with
219 additions
and
59 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the NucleosUserBundle package. | ||
* | ||
* (c) Christian Gripp <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nucleos\UserBundle\Form\Type; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; | ||
use Symfony\Component\Form\Extension\Core\Type\HiddenType; | ||
use Symfony\Component\Form\Extension\Core\Type\PasswordType; | ||
use Symfony\Component\Form\Extension\Core\Type\SubmitType; | ||
use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormError; | ||
use Symfony\Component\Form\FormEvent; | ||
use Symfony\Component\Form\FormEvents; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
use Symfony\Component\Security\Core\Security; | ||
|
||
final class LoginFormType extends AbstractType | ||
{ | ||
/** | ||
* @var RequestStack | ||
*/ | ||
private $requestStack; | ||
|
||
public function __construct(RequestStack $requestStack) | ||
{ | ||
$this->requestStack = $requestStack; | ||
} | ||
|
||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$builder | ||
->add('_username', TextType::class, [ | ||
'label' => 'security.login.username', | ||
'attr' => [ | ||
'autocomplete' => 'username', | ||
], | ||
]) | ||
->add('_password', PasswordType::class, [ | ||
'label' => 'security.login.password', | ||
'attr' => [ | ||
'autocomplete' => 'password', | ||
], | ||
]) | ||
->add('_remember_me', CheckboxType::class, [ | ||
'label' => 'security.login.remember_me', | ||
'required' => false, | ||
'value' => 'on', | ||
]) | ||
->add('_target_path', HiddenType::class) | ||
->add('save', SubmitType::class, [ | ||
'label' => 'security.login.submit', | ||
]) | ||
; | ||
|
||
$request = $this->requestStack->getCurrentRequest(); | ||
|
||
$builder->addEventListener(FormEvents::PRE_SET_DATA, static function (FormEvent $event) use ($request): void { | ||
if (null === $request) { | ||
return; | ||
} | ||
|
||
$error = null; | ||
|
||
if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) { | ||
$error = $request->attributes->get(Security::AUTHENTICATION_ERROR); | ||
} else { | ||
$error = $request->getSession()->get(Security::AUTHENTICATION_ERROR); | ||
} | ||
|
||
if (null !== $error) { | ||
$event->getForm()->addError(new FormError($error->getMessage())); | ||
} | ||
|
||
$event->setData(array_replace((array) $event->getData(), [ | ||
'username' => $request->getSession()->get(Security::LAST_USERNAME), | ||
])); | ||
}); | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver->setDefaults([ | ||
'translation_domain' => 'NucleosUserBundle', | ||
]); | ||
} | ||
|
||
public function getBlockPrefix(): string | ||
{ | ||
return ''; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the NucleosUserBundle package. | ||
* | ||
* (c) Christian Gripp <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nucleos\UserBundle\Form\Type; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\SubmitType; | ||
use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
final class RequestPasswordFormType extends AbstractType | ||
{ | ||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$builder | ||
->add('username', TextType::class, [ | ||
'label' => 'resetting.request.username', | ||
'attr' => [ | ||
'autocomplete' => 'username', | ||
], | ||
]) | ||
->add('save', SubmitType::class, [ | ||
'label' => 'resetting.request.submit', | ||
]) | ||
; | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver->setDefaults([ | ||
'translation_domain' => 'NucleosUserBundle', | ||
]); | ||
} | ||
|
||
public function getBlockPrefix(): string | ||
{ | ||
return ''; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,7 @@ | ||
{% trans_default_domain 'NucleosUserBundle' %} | ||
|
||
<form action="{{ path('nucleos_user_resetting_send_email') }}" method="POST" class="resetting-form"> | ||
<div class="form-group"> | ||
<label for="username">{{ 'resetting.request.username'|trans }}</label> | ||
<input type="text" id="username" name="username" required="required" class="form-control" /> | ||
</div> | ||
{{ form_errors(form) }} | ||
|
||
<div> | ||
<button type="submit" class="btn btn-primary"> | ||
{{ 'resetting.request.submit'|trans }} | ||
</button> | ||
</div> | ||
</form> | ||
{{ form_start(form) }} | ||
{{ form_widget(form) }} | ||
{{ form_end(form) }} |
Oops, something went wrong.